\n\n\nFor scripts with independence from the rest of the page, use async, enabling the browser to persist in page rendering while the script downloads.\n\nhtml\n\n\nBoth these attributes critically improve page speed and overall user experience.","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Nikita Barsukov","url":"https://explain.codes//author/nikita-barsukov"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2024-09-15T23:00:01.485Z","dateModified":"2024-09-15T23:00:03.084Z"}
Explain Codes LogoExplain Codes Logo

Where should I put `

web-development
script-loading
async-defer
performance-optimization
Nikita BarsukovbyNikita Barsukov·Sep 15, 2024
TLDR

<script> tags are best positioned right before </body> to circumvent blocking of the page's rendering. When scripts depend on the HTML being fully parsed, use defer to postpone their execution until the document has finished loading.

<script src="your-script.js" defer></script>

For scripts with independence from the rest of the page, use async, enabling the browser to persist in page rendering while the script downloads.

<script src="independent-script.js" async></script>

Both these attributes critically improve page speed and overall user experience.

Detailed breakdown of script loading behaviors

Async vs Defer: What’s the difference?

The efficient loading of scripts necessitates the placement of <script async> or <script defer> tags in the <head>. The async command facilitates simultaneous downloading and parsing of scripts, albeit with the risk of scripts being executed at unpredictable times. This is best leveraged for discrete scripts, where there are no interdependencies.

Conversely, <script defer> maintains the sequential execution of scripts, launching only once the whole document parsing is complete. This is useful when scripts are dependent on the original DOM structure, or on the execution of preceding scripts.

Inline scripts and parser blocking

Inline <script> tags lack async or defer, and will pause document parsing, ideally used only when scripts must be executed instantly. Such cases include critical feature pollyfills that the entire page relies on.

Swift and space-saving script hosting

For efficient page loading, compression and minification of script files is recommended, utilizing the features of HTTP/2 for improved resource loading. To manage dependencies and postpone execution, script type modules (<script type="module">) may also be utilized.

Script management: strategic execution and placement

The order of things: handling script dependencies

Dependencies and execution order play a big part in script management. To ensure that scripts run in order after the complete parsing of the document, use defer.

Taming third-party scripts

For external scripts such as Google Maps API, deploy async. A callback mechanism can ensure functionality starts only once script loading is complete.

Exploiting caching for repeat visits

Implement caching strategies to stop re-fetching static scripts every visit. This improves load times dramatically on return visits.

Reducing HTTP requests through resource bundling

Bring multiple scripts together into single files to shrink the number of HTTP requests. HTTP/2’s multiplexing can also benefit from more granular script splitting.