How to Create a Script Element in JavaScript

To create a script element in JavaScript:

  1. Use the document.createElement() method to create the script element.
  2. Set the src attribute on the element object to a script file.
  3. Include the script element in the HTML using the appendChild() method.

Consider this sample HTML markup:

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Coding Beauty Tutorial</title>
  </head>
  <body>
    <div id="box"></div>

    <script src="index.js"></script>
  </body>
</html>

Here’s how we can use JavaScript to create a script element in the HTML:

index.js

const script = document.createElement('script');

// use local file
// script.src = 'script.js';

script.src =
  'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js';

script.async = true;

// make code in script to be treated as JavaScript module
// script.type = 'module';

script.onload = () => {
  console.log('Script loaded successfuly');
  const box = document.getElementById('box');
  box.textContent = 'The script has loaded.';
};

script.onerror = () => {
  console.log('Error occurred while loading script');
};

document.body.appendChild(script);

The document.createElement() method creates an HTML element specified by the tag name and returns the element. By passing a script tag, we create a script element.

const script = document.createElement('script');

We set the src property on the script element to specify the script file to be loaded. We specify a remote file with a URL, but we could also specify a local file with a relative or absolute file path.

// use local file
// script.src = 'script.js';

script.src =
  'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js';

By setting the async property to true, the browser won’t have to load and evaluate the script before continuing to parse the HTML. Instead, the script file will be loaded in parallel to reduce delays and speed up the processing of the page.

script.async = true;

The type attribute indicates what type of script the file is. If it is a JavaScript module, we’ll need to set the type attribute to module to show this.

script.type = 'module';

For a complete list of all attributes supported by the script element, visit the MDN docs on the script element.

We listen for the onload event in order to perform an action when the script file has been loaded successfully.

script.onload = () => {
  console.log('Script loaded successfuly');
  const box = document.getElementById('box');
  box.textContent = 'The script has loaded.';
};

We listen for the onerror event so that we can perform a different action in case there was an error with loading the script.

script.onerror = () => {
  console.log('Error occurred while loading script');
};

The appendChild() method adds a DOM element as the last child of a specified parent element. By calling appendChild() on document.body, we add the script file to the body.

document.body.appendChild(script);
The script file is included in the body of the document.
The script file is included in the body of the document.

To add the script file to the head of the document instead, we can replace document.body with document.head.

document.head.appendChild(script);
The script file is included in the head of the document.
The script file is included in the head of the document.


11 Amazing New JavaScript Features in ES13

This guide will bring you up to speed with all the latest features added in ECMAScript 13. These powerful new features will modernize your JavaScript with shorter and more expressive code.

11 Amazing New JavaScript Features in ES13

Sign up and receive a free copy immediately.

2 thoughts on “How to Create a Script Element in JavaScript”

  1. const script = document.createElement(‘script’);
    script.src = ‘https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js’;
    script.async = true;
    document.body.appendChild(script);
    gives me:
    VM48:1 Refused to load the script ‘https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js’ because it violates the following Content Security Policy directive: “script-src chrome://resources chrome://test chrome://webui-test ‘self'”.
    am i doing something wrong?

Leave a Comment

Your email address will not be published. Required fields are marked *