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:

%MINIFYHTML166d7d6fd1b914046370bab5520c4f3123%<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