How Javascript Files Load (Normal vs Async vs Defer)

Welcome to a quick tutorial on how Javascript files are loaded – Normal, async, and defer. Ever since day one of learning Javascript, we were taught to either code Javascript inline or load them from external files. But just how are these Javascript files loaded? This simple question has somehow eluded many beginners, and in short point form:

  • Javascript is normally loaded in the order of top-to-bottom, left-to-right. One script has to be fully loaded and executed before the next can proceed.
  • When set to defer, the script will load in the background and run only after the page is loaded (but before DOMContentLoaded).
  • When set to async, the script becomes “independent”. It loads in the background and runs whenever it is ready.

Still confused? Let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Javascript Loading Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

JAVASCRIPT LOADING SEQUENCE

All right, let us now get into the examples of how Javascript actually load and run.

 

1) NORMAL LOAD

THE EXAMPLE

1-normal.html
<script src="first.js"></script>
<script src="second.js"></script>
<p>Rest of the page.</p>
first.js
console.log("First");
second.js
console.log("Second");

As in the introduction above, HTML pages load from top-to-bottom, left-to-right. The same rules apply to Javascript:

  • first.js will load and run first.
  • Followed by second.js.
  • Then the rest of the page will load accordingly.

 

BLOCKING PROBLEM

Once upon a time, the Internet was simple. This sequential way of loading did not pose any problems. But as things grow complicated, loading massive Javascript also becomes a huge issue. Basically, we have to wait for it to finish loading and running before the rest of the page can continue.

As you can guess, nobody likes to stare at an empty “now loading” screen, even for a few seconds. This is called “blocking Javascript”, and 2 mechanisms are introduced to solve it – defer and async.

 

 

2) DEFER LOAD

THE EXAMPLE

2-defer.html
<script defer src="first.js"></script>
<script src="second.js"></script>
<p>Rest of the page.</p>

Look no further, we have only added defer to first.js. But that makes a world of difference in the loading behavior of the Javascript:

Yes, first.js has now broken away from the “normal sequential order”.

  • first.js will load in the background, while the rest of the page continues.
  • It will only execute when the page is done loading, right before DOMContentLoaded. For those who are new, this is the point where all the HTML elements are loaded, but not necessarily all the contents. I.E. HTML tags are fully loaded, but images and videos may not be.

 

DEFER CAN STILL BE BLOCKING

What!? Didn’t first.js already load in the background? How is this still blocking? Consider the following situation:

  • The HTML page finish loading before first.js.
  • The browser still has to wait for first.js to finish loading before proceeding.

So there you go, don’t be mistaken. defer can still be blocking.

 

 

3) ASYNCHRONOUS LOAD

THE EXAMPLE

3-async.html
<script async src="first.js"></script>
<script src="second.js"></script>
<p>Rest of the page.</p>

Look no further, we have only changed async on first.js. Things get a little funky with asynchronous…

For those who are lost, first.js is now “independent”. It simply loads in the background and runs when fully loaded. Yes, that could be before the HTML page is loaded, or it could be after the page is loaded.

While this is cool and does not block the page, I have a love-hate relationship with async. The timing is hard to catch and can often break the entire page when used incorrectly.

 

DEFER VS ASYNC

The above should have explained clearly enough, but in simple point form:

  • defer and async both loads the Javascript in the background.
  • defer runs before DOMContentLoaded, async runs whenever it’s ready.
  • defer can be blocking, async does not.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

ASYNC & DEFER TOGETHER?

Sadly, this is kind of the confusion zone. From what I gather from the Internet:

  • Older browsers will only understand defer.
  • Modern browsers will honor async, it has priority.
  • Some smarter browsers will automatically honor defer, run on DOMContentLoaded. But if the script is still loading at the point of DOMContentLoaded, it will turn async and run only when fully loaded.

 

INFOGRAPHIC CHEAT SHEET

How Javascript Files Load (Click to Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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