|HTML parsing paused|
The browser will begin to download the deferred scripts in parallel without stopping page processing (during HTML parsing) and will only execute them after the parser has completed. A positive effect of this attribute is that the DOM will be available for the scripts.
The scripts downloaded with
defer are also guaranteed to execute in the order that they appear in the document.
async is identical to
defer, except that the script executes at the first opportunity after download (will pause the HTML parser to execute it when it has finished downloading).
When should I use what?
Typically you want to use
async where possible, then
defer then no attribute.
Some general rules to follow:
- if the script is modular and does not rely on any scripts then use
- if the script relies upon or is relied upon by another script then use
- if the script is small and is relied upon by an
asyncscript then use an inline script with no attributes placed above the
- async vs defer attributes
Categories & Tags