Files
pjax/lib/eval-script.js

42 lines
1.0 KiB
JavaScript
Raw Normal View History

2014-05-04 08:44:18 +02:00
module.exports = function(el) {
// console.log("going to execute script", el)
2014-05-04 08:44:18 +02:00
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
2018-01-29 23:20:00 -05:00
if (src !== "") {
script.src = src
2018-02-01 08:45:53 +00:00
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
2018-01-29 23:20:00 -05:00
if (code !== "") {
2017-11-02 12:40:30 +01:00
try {
script.appendChild(document.createTextNode(code))
}
catch (e) {
// 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
2018-01-29 23:20:00 -05:00
if (["head", "body"].indexOf(parent.tagName.toLowerCase()) > 0) {
parent.removeChild(script)
}
2014-05-04 08:44:18 +02:00
return true
2014-05-04 08:44:18 +02:00
}