- When set to
defer, the script will load in the background and run only after the page is loaded (but before
- 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
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.
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.
1) NORMAL LOAD
<script src="first.js"></script> <script src="second.js"></script> <p>Rest of the page.</p>
first.jswill load and run first.
- Followed by
- Then the rest of the page will load accordingly.
2) DEFER LOAD
<script defer src="first.js"></script> <script src="second.js"></script> <p>Rest of the page.</p>
Look no further, we have only added
first.js has now broken away from the “normal sequential order”.
first.jswill 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
first.js already load in the background? How is this still blocking? Consider the following situation:
- The HTML page finish loading before
- The browser still has to wait for
first.jsto finish loading before proceeding.
So there you go, don’t be mistaken.
defer can still be blocking.
3) ASYNCHRONOUS LOAD
<script async src="first.js"></script> <script src="second.js"></script> <p>Rest of the page.</p>
Look no further, we have only changed
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:
asyncruns whenever it’s ready.
defercan be blocking,
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
- 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
asyncand run only when fully loaded.
INFOGRAPHIC CHEAT SHEET
LINKS & REFERENCES
- Script Element – MDN
- Load a script file asynchronously – PlainJS
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!