Files
pjax/lib/eval-script.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-05-04 08:44:18 +02:00
module.exports = function(el) {
var code = (el.text || el.textContent || el.innerHTML || "")
var src = (el.src || "")
var parent = el.parentNode || document.querySelector("head") || document.documentElement
2014-05-04 08:44:18 +02:00
var script = document.createElement("script")
if (code.match("document.write")) {
if (console && console.log) {
console.log("Script contains document.write. Cant be executed correctly. Code skipped ", el)
}
return false
}
script.type = "text/javascript"
2017-11-02 12:40:30 +01:00
/* istanbul ignore if */
if (src !== "") {
script.src = src
script.async = false // force synchronous loading of peripheral JS
2014-05-04 08:44:18 +02:00
}
2017-11-02 12:40:30 +01:00
if (code !== "") {
2017-11-02 12:40:30 +01:00
try {
script.appendChild(document.createTextNode(code))
}
catch (e) {
/* istanbul ignore next */
2017-11-02 12:40:30 +01:00
// old IEs have funky script nodes
script.text = code
}
2014-05-04 08:44:18 +02:00
}
// execute
parent.appendChild(script)
// avoid pollution only in head or body tags
if (parent instanceof HTMLHeadElement || parent instanceof HTMLBodyElement) {
parent.removeChild(script)
}
2014-05-04 08:44:18 +02:00
return true
2014-05-04 08:44:18 +02:00
}