Skip to main content

Loading Modules in the Browser

Use:

<script type="module" src="myFile.js"></script>

The defer attribute is automatically implied. defer allows the browser to download and parse the script in parallel with HTML parsing. The script is executed after the document has been parsed, but before DOMContentLoaded.

Scripts will execute in the order they appear in the document.

The async attribute

Use the async attribute like this:

<script type="module" src="myFile.js" async></script>

The async attribute causes the script to execute as soon as it’s been downloaded. Interrupting HTML parsing if necessary.

This disables ‘in-order’ execution of the script. It will execute as soon as possible.

More info