JS: Load Order

By Xah Lee. Date: . Last updated: .

This page shows the basics of JavaScript execution order, and techniques to run JavaScript deferred or asynchronous.

js async defer
JavaScript fetch and execute order for “normal”, and defer, async.

When a JavaScript appears in a HTML page:

<script src="…"></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.

HTML async / defer Attributes

In HTML5, you can tell browser when to run your JavaScript code. There are 2 new possibilities:

<script defer src="file.js"></script>
<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. This is so to support older browsers that doesn't support async.

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

Warning: When using defer or async, you must have a src="…". You cannot use it like this: <script defer>…</script>.

As of , all major browser support async.

Multiple async scripts's execution order

Multiple async scripts's execution order is not guaranteed.

Multiple defer scripts's execution order

Multiple defer scripts's execution order is by the order they appear on HTML page.

Deferred Loading JavaScript for Old Browser

JS: 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(){

Web Scripting Overview

  1. Browser Window Object, DOM
  2. Intro to Event-Based Programing
  3. JavaScript Load Order

DOM How-To

  1. Basic DOM Methods
  2. Get Element by ID, Tag, Name, Class, CSS
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Attribute Value
  8. Set Attribute Value
  9. Remove Attribute
  10. List/Add/Remove Class Attribute
  11. Add/Remove Event Handler
  12. Navigate DOM Tree
  13. Node Type, Name, Value

HTML Input

  1. Button
  2. Text Field
  3. Password Field
  4. Textarea
  5. Email 📧
  6. Number Field
  7. Radio Button 🔘
  8. Checkbox 🗹
  9. Popup Menu
  10. Slider 🎚
  11. Color Picker 🌈

Web Scripting Examples

  1. Disable Right Click
  2. document.write
  3. Image Rollover
  4. Pop-up New Window
  5. Digital Clock
  6. Stopwatch
  7. Fade a Element
  8. How to Create Tooltip
  9. Falling Snow Effect
  10. Box Following Scroll

Web Scripting Misc

  1. Web Cookies
  2. Web Storage
  3. DOM: Open URL window.open
  4. Encode URL, Escape String
  5. Browser Info, Navigator Object
  6. Get URL (window.location)
  7. Find Window Width
  8. Find Element Width
  9. WebSocket
  10. Value of “this” in Event Handler
  11. Event Delegation
  12. Get Current Script Tag
  13. insertAfter Element
  14. Randomize List
  15. create Document Fragment
  16. innerHTML, nodeValue, textContent, innerText?
  17. What is Live Object
  18. NodeList vs HTMLCollection
  19. Whitespace Nodes


  1. jQuery Basics
  2. jQuery() vs querySelectorAll()
  3. Write JQuery Plugin


Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog