JavaScript: Load Order, defer, async, module

By Xah Lee. Date: . Last updated: .

When a JavaScript appears in a HTML page:

<script src="some.js"></script>



the browser will fetch and execute the script right away, and wait for it to finish, before loading any HTML that appear after the script tag.

This means:

Old solution (2005) is to always place your JavaScript at the bottom of the page, right before the </body> tag. But this is not a good solution today.

defer Load

You can use the attribute defer like this:

<script defer src="file.js"></script>

defer means run script after the page finished parsing.

Warning: there is no “=true” or “=false” after async or defer.

Warning: When using defer or async, you must have a src=val. You cannot use it like this: <script defer>code</script>. That'll behave the same as without defer/async.

As of , all major browser support defer.

The execution order of multiple defer scripts, is the order they appear on HTML page.

async Load

async (asynchronous) means run the script and continue loading the HTML page at the same time.

<script async src="file.js"></script>

Warning: with async, your script will not see HTML elements that are below the script tag, because they are not loaded yet.

If both async and defer are specified, it behaves as if only defer is specified.

The execution order of multiple async scripts is not specified. (not guaranteed to be in the order on the page.)

As of , all major browser support async.

Module Script Load Order

When type="module" script is used, it loads like defer.

Optionally you can specify async loading.

[see Import/Export]

js async defer 6hhq9
JavaScript fetch and execute order [image source , © ]

Deferred Loading JavaScript for Old Browser

Deferred Loading JavaScript for Old Browser

Load Event: Wait for the Whole Page to Load

An old way to load script (used before ~2010), is to use the window.onload event.

This is not a good solution, because it waits until all images and iframes finished downloading too.

You'll see old code like this:

function f1 () {console.log( "f1 called");};
function f2 () {console.log( "f2 called");};

window.onload = function(){
JavaScript in Depth

DOM Scripting


DOM Methods

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc