Here is the JS code:
var wrap = document.createElement("div");
wrap.innerHTML = '<script type="text/javascript" src="'+scriptUrl+'"></script>';
var wrapscript = wrap.childNodes[0];
document.body.appendChild(wrapscript)
The body did insert the script element, but the JS resource wasn’t loaded, there isn’t even an http request.
Could someone explain why this is happening?
The problem is with Zeptojs’s $ method
$('<script type="text/javascript" src="'+scriptUrl+'"></script>').appendTo($("bdoy"))
It works like the code above, and causes the bug.
This one was trivial.
As stated in spec (8.4 Parsing HTML fragments and 8.2.3.5 Other parsing state flags,) quote:
when using
innerHTMLthe browser willand when parsing a
<script>insideSo it won’t be executed, as long as you inject it with
innerHTML.And using
innerHTMLwill prevent the<script>element created from being executed permanently.As stated in spec (4.3.1 The script element,) quote:
Concluding the described below is that, it only parse the
srcattribute when injecting the<script>to thedocument(no matter which, including the temporary one created when usinginnerHTML.)So, as long as you want to inject a script to the document and make it executed, you have to use
script = document.createElement('script').Set its attributes like
srcandtype, possibly the contents inside (by usingscript.appendChild(document.createTextNode(content))), then append it to thedocument.body.