How to append <script></script> in JavaScript? [duplicate] How to append <script></script> in JavaScript? [duplicate] javascript javascript

How to append <script></script> in JavaScript? [duplicate]


// Create the elementvar script = document.createElement("script");// Add script contentscript.innerHTML = "...";// Appenddocument.head.appendChild(script);

Or

document.body.appendChild(script);


Try this:

var s = document.createElement("script");s.type = "text/javascript";s.src = "http://somedomain.com/somescript";$("head").append(s);

Note that the script will load and you can access the variables inside it, but you wouldn't see the actual <script> tag in the DOM.


$('<script>alert("hi");</' + 'script>').appendTo(document.body);

DEMO