
Dynamically Create a Script Element with JavaScript

This Article is Over Ten Years Old...
Things can and do move very quickly in tech, which means that tech-related articles go out of date almost as soon as they have been written and published. If you are looking for up-to-date technical advice or opinion, it is unlikely that you will find it on this page.
You may find that my recent articles are more relevant, and you are always welcome to drop me a line if you have a specific technical problem you are trying to solve.
There are many reasons why you might want to dynamically inject a <script> tag into your document from the client side. Here, I very briefly discuss how we can use the document.createElement() method, creating a new element and then setting its src attribute to the URL of the script we want to load. Finally, we can append the newly‑generated element to the HTML page via either document.head or document.body.
For example, let's say we want to load the script example.js and add it to the head of the HTML page. The code would look something like this:
var script = document.createElement('script');script.src = 'example.js';document.head.appendChild(script);The resulting markup would look like this:
<script src="example.js" />It really is that simple. Naturally, you're going to want to use some sort of document load detection to make sure that the document has actually loaded before initiating the dynamic injection.
You can also set innerHTML of the script element, rather than using an external file via src. This allows you to dynamically inject rudimentary JavaScript code into the head of your page.
var script = document.createElement('script');script.innerHTML = "console.log('example')";document.head.appendChild(script);//=> 'example'This generates an element in the head that looks like this (and which will run immediately):
<script> console.log('example');</script>..and that is as simple as it gets!
Categories:
Related Articles

How to Read JavaScript Errors and Stack Traces. 
Five Tips on How to Be a Good Web Developer. Five Tips on How to Be a Good Web Developer

Using CommonJS to Implement Modules in JavaScript. Using CommonJS to Implement Modules in JavaScript

Throttling Scroll Events in JavaScript. Throttling Scroll Events in JavaScript

Dynamic Calculations in CSS Using calc(). Dynamic Calculations in CSS Using
calc()
Exploring the call() Method in JavaScript. Exploring the
call()Method in JavaScript
LeetCode: Solving the 'Merge Two Sorted Lists' Problem. LeetCode: Solving the 'Merge Two Sorted Lists' Problem

Extends and super in JavaScript Classes. extendsandsuperin JavaScript Classes
Converting Between Camel, Snake, and Kebab Case in JavaScript. Converting Between Camel, Snake, and Kebab Case in JavaScript

Classes in JavaScript: An Introduction. Classes in JavaScript: An Introduction

Using Regex to Replace Numbers in a String. Using Regex to Replace Numbers in a String

Why querySelector Returns null in JavaScript. Why
querySelectorReturnsnullin JavaScript