Files
pjax/lib/eval-script.js

49 lines
1.2 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;
var script = document.createElement("script");
2014-05-04 08:44:18 +02:00
if (code.match("document.write")) {
if (console && console.log) {
console.log(
"Script contains document.write. Cant be executed correctly. Code skipped ",
el
);
2014-05-04 08:44:18 +02:00
}
return false;
2014-05-04 08:44:18 +02:00
}
script.type = "text/javascript";
script.id = el.id;
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;
2017-11-02 12:40:30 +01:00
}
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.contains(script)
) {
parent.removeChild(script);
}
2014-05-04 08:44:18 +02:00
return true;
};