Notes & TILs
Search…
`async` & `defer` Attributes
Usually javascript files interrupt the parsing of HTML document. To prevent this 2 special attributes of the <script> elements are used.

async

1
<script async src="script.js">
Copied!
  • The async attribute is used to indicate to the browser that the script file can be executed asynchronously.
  • Therefore the HTML parser does not need to pause and wait for the JS code to load, it is intead fetched in parallel.
  • It is only available for externally located script files.

defer

1
<script defer src="script.js">
Copied!
  • The defer attribute tells the browser to only execute the script file once the HTML document has been fully parsed.
  • The js file can be downloaded but it does not executes until the whole HTML is parsed.
Last modified 1mo ago
Copy link
Contents
async
defer