146

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?

simhumileco
  • 31,877
  • 16
  • 137
  • 115
David
  • 16,246
  • 34
  • 103
  • 162

6 Answers6

305
// Create the element

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

// Add script content

script.innerHTML = "...";

// Append

document.head.appendChild(script);

Or

document.body.appendChild(script);
gaby de wilde
  • 1,313
  • 12
  • 6
Dennis
  • 14,264
  • 2
  • 48
  • 57
173

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.

Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
  • 11
    Inspect element and you'll see – James Apr 02 '16 at 23:10
  • 7
    This method generates a warning that loading scripts synchronously is detrimental to user experience. Assuming you're going to want to be calling something in that javascript you're loading, it should be loaded asynchronously and a callback method employed to run your code which is dependant on the new script. http://stackoverflow.com/questions/7718935/load-scripts-asynchronously#7719185 – ThisLeeNoble May 24 '16 at 13:45
  • You can set async to true. – Michael Rogers Dec 26 '19 at 01:54
  • 1
    `s.type` is not needed, the `shorter` the `better`. – Timo Mar 15 '21 at 09:07
  • 1
    @Timo `type` is required for generating valid HTML if you use an XHTML doctype. – Protector one Jan 03 '22 at 13:08
  • 1
    @Timo is right you don't need to define defaults similar input default is type="text", with that said if you have other scripts in document.head that are not of type javascript than good to include to differentiate, but chances are you are not, your script tag always javascript, therefore redundant to be explicit – PDA Jan 28 '23 at 01:34
56
$('<script>alert("hi");</' + 'script>').appendTo(document.body);

DEMO

qwertymk
  • 34,200
  • 28
  • 121
  • 184
  • Cool but how does this work? I've known this fix for about a year and still have no clue whatsoever. – Boyan Nov 23 '14 at 21:35
  • 22
    The only reason you can't do `$('')` is because the string `` isn't allowed inside javascript because the DOM layer can't parse what's js and what's html. You can even do `$(' – qwertymk Nov 23 '14 at 22:55
38

If you need to append a script so that it's parsed you could do as google does for his +1 button

  (function() {
    var po = document.createElement('script');
    po.type = 'text/javascript';
    po.async = true;
    po.src = 'link to your script here';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
  })();
Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
30

This worked for me..

<script>
    $('head').append("<script>your script content here<\/script>");
</script>

Note that the "/" of the inner </script> has been escaped to be "<\/script>". If it is not, it actually closes the outer <script> tag.

The script added wont be visible but will get executed. Hope that helps.

DAC84
  • 1,254
  • 1
  • 20
  • 30
Suganya
  • 574
  • 6
  • 9
2
$('your_document_selector').text('<script></script>')

.text() makes it possible to append script tags with it being rendered as HTML.

OR

$('your_document_selector').append('&lt;script&gt;&lt;/script&gt;')
linuxeasy
  • 6,269
  • 7
  • 33
  • 40