in JavaScript? [duplicate]", "text": "<p>I need to use <code>appendChild()</code> or jQuey's <code>append()</code> to append some <code>&lt;script&gt;</code> tag stuff into the document. From what I can tell, this is getting stripped out. Anyone know how to do it?</p>", "answerCount": 2, "upvoteCount": 699, "dateCreated": "2022-10-17 18:58:17", "dateModified": "2022-10-20 06:58:18", "author": { "type": "Person", "name": "David" }, "acceptedAnswer": { "@type": "Answer", "text": "<pre class="prettyprint"><code>// Create the element\n\nvar script = document.createElement("script");\n\n// Add script content\n\nscript.innerHTML = "...";\n\n// Append\n\ndocument.head.appendChild(script);\n</code></pre>\n\n<p>Or</p>\n\n<pre class="prettyprint"><code>document.body.appendChild(script);\n</code></pre>", "upvoteCount": 293, "url": "https://exchangetuts.com/how-to-append-scriptscript-in-javascript-duplicate-1639495925246056#answer-1666292297999263", "dateCreated": "2022-10-19 18:58:17", "dateModified": "2022-10-20 06:58:17", "author": { "type": "Person", "name": "Dennis" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>Try this: </p>\n\n<pre class="prettyprint"><code>var s = document.createElement("script");\ns.type = "text/javascript";\ns.src = "http://somedomain.com/somescript";\n$("head").append(s);\n</code></pre>\n\n<p>Note that the script will load and you can access the variables inside it, but you wouldn't see the actual <code>&lt;script&gt;</code> tag in the DOM.</p>", "upvoteCount": 167, "url": "https://exchangetuts.com/how-to-append-scriptscript-in-javascript-duplicate-1639495925246056#answer-1666292298004327", "dateCreated": "2022-10-18 18:58:18", "dateModified": "2022-10-20 06:58:18", "author": { "type": "Person", "name": "Sapan Diwakar" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 699
David Avatar asked Oct 17 '22 18:10

David


People also ask

How do you append a script tag to head?

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.

Can I have 2 scripts in HTML?

An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.

How do you put a script inside a script tag?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.


2 Answers

// Create the element

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

// Add script content

script.innerHTML = "...";

// Append

document.head.appendChild(script);

Or

document.body.appendChild(script);
like image 293
Dennis Avatar answered Oct 20 '22 06:10

Dennis


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.

like image 167
Sapan Diwakar Avatar answered Oct 20 '22 06:10

Sapan Diwakar