Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create script tag by jQuery?

I want to create a script tag by jQuery.

I use the following code:

$("<body>").append("<script></script>"); 

It doesn't work. What will you do to achieve it?

like image 390
Billy Avatar asked Jul 29 '09 11:07

Billy


People also ask

Can we write jQuery in script tag?

Step 1: Open the HTML file in which you want to add your jQuery with the help of CDN. Step 2: Add <script> tag between the head tag and the title tag which will specify the src attribute for adding your jQuery. Step 3: After that, you have to add the following path in the src attribute of the script tag.

Does jQuery go inside script tag?

We can include the jQuery CDN URL in the script tag and use jQuery in HTML. We can either write the jQuery in a separate . js file and include it in the HTML file or write the jQuery inside the script tag. First, go to the JQuery CDN website and choose the latest stable version of the jQuery.

Can we write jQuery in HTML?

You can write it in side the tag, where you would write the javascript usually. Show activity on this post. You can write it directly to the HTML doc inside <script> tags or link with <script src="javascript.


2 Answers

You should do it like so

var script=document.createElement('script'); script.type='text/javascript'; script.src=url;  $("body").append(script); 
like image 161
Cristian Toma Avatar answered Sep 30 '22 02:09

Cristian Toma


To load from a url:

$("body").append($("<script />", {   src: url })) 

To load from existing source code:

$("body").append($("<script />", {   html: code })) 
like image 40
Daniel X Moore Avatar answered Sep 30 '22 02:09

Daniel X Moore