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:

%MINIFYHTML7886e9990d4c6d3a1200ff647e52561f23%<script src="scripts/app.js" defer></script>Code language: HTML, XML (xml)

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>Code language: HTML, XML (xml)

About Author /

Leave a Comment

Your email address will not be published.

Start typing and press Enter to search