JS: Defer & Async

When loading HTML with Javascript in the page, we can use the defer option in the Script tag. This allows the browser load the scripts, but to parse all of the HTML first, and only then run the Javascript tags.

The syntax for defer is:

<script src="scripts/app.js" defer></script>

Async is another option. This parameter tells the browser to load the script tag as soon as possible and not block the HTML parsing. However, Javascript is run once it’s downloaded. As Javascript is a single threaded language, the HTML parsing will pause while the Javascript is executed. Once complete it would resume HTML parsing. The syntax is:

<script src="scripts/app.js" async></script>

Leave a Reply

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