attribute of the
(supported by all major browsers since about )
<script async src="file.js"></script>
<script defer src="file.js"></script>
For older browser that doesn't support
attributes, you can
will run after the DOM is ready.
script element and its contents, then insert this node into DOM.
var ele = document .createElement("script"); ele.src = "xyz.js"; document .body.appendChild(ele);
Test your browser: js_insert_js_works.html
Putting Script Tag in innerHTML Does Not Work
Suppose you do it this way:
// create new div element var xx = document .createElement("div"); // create script tag as div's content xx.innerHTML='<script>alert("yay!");<\/script>'; // add to DOM document .body.appendChild(xx);
As of , it will fail in Google Chrome, Firefox.
Test your browsor: js_insert_js.html
If you have a question, put $5 at patreon and message me.