How to Create a Script Tag in jQuery?

Estimated read time 1 min read

You can create a script tag using jQuery with the following code:

var scriptTag = $("<script></script>");
scriptTag.attr("src", "path/to/script.js");
$("head").append(scriptTag);

In this code, we first create a new script tag using the $("<script></script>") syntax. Then, we use the attr() method to set the src attribute of the script tag to the path of the script file we want to load. Finally, we append the script tag to the head element of the HTML document using the append() method.

This code will create a new script tag with the specified src attribute and append it to the head element of the document. When the script is loaded, it will be executed by the browser.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply