How to Append <script></script> in javascript?

David picture David · Feb 23, 2012 · Viewed 324.5k times · Source

I need to use childappend or jquery append() to append some tag stuff into the document. From what I can tell, this is getting stripped out. Anyone know how to do it?

Answer

Dennis picture Dennis · Feb 23, 2012
// Create the element

var script = document.createElement("script");

// Add script content

script.innerHTML = "...";

// Append

document.head.appendChild(script);

Or

document.body.appendChild(script);