MA

  • Home
  • About

Loading Modules in the Browser

July 2021

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

  • The <script> element on MDN
  • <script> cheatsheet
  • JavaScript modules on v8.dev

  • Next: Optimising CI Runs
  • Previous: Pseudo "Components" for CSS
RSS Feed Edit this page on Github