The short answer
#Both defer and async let the browser download a script while it keeps parsing the HTML. The difference is when the script runs. A defer script waits until the document has been parsed and runs in the order it appears. An async script runs as soon as it has downloaded, in whatever order the downloads finish. Use defer for your own application code and async for independent scripts such as analytics.
_10<head>_10 <!-- your app: runs in order, after parsing -->_10 <script defer src="/js/vendor.js"></script>_10 <script defer src="/js/app.js"></script>_10_10 <!-- independent: runs whenever it is ready -->_10 <script async src="https://example.com/analytics.js"></script>_10</head>
Why it happens
#A plain script tag with a src is parser-blocking. When the browser reaches it, it stops building the page, downloads the file, runs it and only then carries on. That is why scripts used to go at the end of the body. Scripts block because they can call document.write or read the DOM that has been built so far, and the browser must respect that.
The two attributes tell the browser the script does not need that guarantee:
| Downloads | Runs | Order | |
|---|---|---|---|
| no attribute | blocks parsing | immediately | document order |
defer | in parallel | after parsing, before DOMContentLoaded | document order |
async | in parallel | as soon as downloaded, pausing the parser | first to arrive |
So defer behaves like putting the script at the end of the body, but the download starts much earlier because the browser sees the tag in the head. async is the fastest to run, but you cannot rely on the DOM being complete or on another script having run first.
How to fix it
#Use defer for scripts that depend on each other or on the DOM
If app.js uses a library loaded by vendor.js, or queries elements on the page, defer keeps the order and guarantees the document is parsed. You do not need a DOMContentLoaded listener inside a deferred script; the DOM is already there.
_10<script defer src="/js/vendor.js"></script>_10<script defer src="/js/app.js"></script>
Use async for scripts that stand alone
Analytics, ads and error reporting do not depend on your code and nothing depends on them. async lets them start as early as possible.
_10<script async src="https://example.com/analytics.js"></script>
Know that modules are deferred by default
A script with type="module" is deferred automatically, so adding defer does nothing. You can add async to a module script to run it as soon as it and its imports are ready.
_10<script type="module" src="/js/main.js"></script>_10<script type="module" async src="/js/widget.js"></script>
Scripts added with JavaScript are async
A script element created with document.createElement and appended to the page behaves like async. Set script.async = false if you need several dynamically added scripts to run in order.
_10const script = document.createElement('script');_10script.src = '/js/chart.js';_10script.async = false;_10document.head.append(script);
Common pitfalls
#- Adding
deferorasyncto an inline script. They only apply to scripts with asrc, exceptasyncon inline module scripts. - Using
asyncfor code that touches the DOM. It may run before the elements it needs exist. - Using
asyncon scripts that depend on each other. The order changes depending on network speed, so the bug comes and goes. - Setting both attributes. Modern browsers treat the script as
async;deferis only a fallback for very old ones. - Listening for
DOMContentLoadedinside anasyncscript. If the script arrives late, the event may already have fired and the listener never runs.
Related reading
#HTMLquick question
Was this resource helpful?