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?
Asked
Active
Viewed 4e+01k times
136
simhumileco
- 27,137
- 16
- 123
- 105
David
- 15,166
- 34
- 100
- 153
-
1the second answer on that question with a vote of 67 is a very good answer which will tell you everything you need to know. – Thomas Clayson Feb 23 '12 at 13:13
-
2simply write `` – Black Apr 23 '18 at 08:22
6 Answers
289
// 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,233
- 12
- 6
Dennis
- 13,786
- 2
- 45
- 57
164
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
- 9,858
- 5
- 31
- 43
-
9
-
6This 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
-
-
1
-
1@Timo `type` is required for generating valid HTML if you use an XHTML doctype. – Protector one Jan 03 '22 at 13:08
-
@Protectorone I do not quite agree, the default is `text/javascript`, check [here](https://web.archive.org/web/20150422034932/http://www.w3.org/TR/html5/scripting-1.html#attr-script-type) – Timo Jan 07 '22 at 19:13
55
-
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
-
22The 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
34
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,233
- 3
- 39
- 41
Nicola Peluchetti
- 74,514
- 30
- 136
- 188
-
nicely done... where should you place this function call? does it matter? `head` `body` – Chef_Code Aug 17 '16 at 19:11
-
-
1This code will crash (s is undefined) if you don't have any script tag in your HTML (example: `
Hello, World! ` - this is valid HTML) – tanguy_k May 26 '21 at 10:56
27
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.
-
I tried every other way with my script, but this is the only way that worked! Thank you very much! – topsoftwarepro Jul 07 '20 at 12:09
-
I tried to append a js widget (with html and a script tag) to a div. This solution worked perfectly! Thank you! – TawabG Dec 30 '21 at 00:16
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('<script></script>')
linuxeasy
- 5,771
- 6
- 31
- 39