Search code examples
javascriptjqueryappendappendchild

How to append <script></script> in JavaScript?


I need to use appendChild() or jQuey's append() to append some <script> tag stuff into the document. From what I can tell, this is getting stripped out. Anyone know how to do it?


Solution

  • 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.