JavaScript
Web Development
HTML
Script Tag
Async & Defer

Script Tag - async & defer

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Web developers use the <script> tag to embed JavaScript code into HTML documents. Understanding the attributes async and defer is crucial for optimizing how scripts are loaded and executed, particularly concerning page load times and user experience.

The Basics: Understanding the <script> Tag

The <script> tag is used to embed a JavaScript resource into an HTML document, either inline within the tag itself or externally via the src attribute. By default, when the browser encounters a <script> tag, it must stop parsing the rest of the HTML until the JavaScript file is fully downloaded and executed. This behavior ensures JavaScript code that manipulates DOM elements is executed only after those elements are parsed. However, this can also slow down the page load time, especially if the script is large or located on a slow server.

Async and Defer Attributes

To mitigate the blocking behavior of JavaScript loading, HTML5 introduced two important attributes: async and defer. Both attributes alter the way the browser loads and executes external JavaScript files, allowing for better control over performance without compromising functionality.

Async Attribute

The async attribute is used to download the script asynchronously alongside the parsing of the rest of the page. Scripts loaded with the async attribute do not prevent the HTML from being parsed but will halt the parser once they need to be executed (whenever they finish downloading).

Example Usage:

html
<script src="path/to/script.js" async></script>

Defer Attribute

The defer attribute, similar to async, allows the script to be downloaded in the background. However, scripts with defer are executed only after the entire document has been parsed. This is particularly useful for scripts that need to interact with the whole DOM or when script execution order matters.

Example Usage:

html
<script src="path/to/script.js" defer></script>

Differences Between Async and Defer

While both async and defer are designed to speed up page loading times by downloading scripts asynchronously, their execution timing is different. Here's a breakdown:

AttributeDownload TypeExecution TimingOrder of Execution
AsyncAsynchronousAs soon as downloaded, interrupts parserNo guaranteed order
DeferAsynchronousAfter document parsing, before DOMContentLoaded eventOrder in the document

Considerations and Best Practices

Choosing between async and defer depends largely on the nature of the JavaScript and how it interacts with your HTML document. Some considerations include:

  • Use async: For scripts that do not rely on other scripts and do not modify the DOM. They’re ideal for tracking scripts, ads, or any script where execution order is not crucial.
  • Use defer: For scripts that depend on the complete parsing of HTML and in cases where script execution order must be maintained. This generally includes large scripts or those that depend on other scripts loaded with defer.

Practical Example

Consider a webpage that includes multiple scripts affecting the page's content:

html
<script src="js/vendor/jquery.js" defer></script>
<script src="js/plugins.js" defer></script>
<script src="js/main.js" defer></script>

Using defer ensures that jQuery loads before plugins.js and plugins.js before main.js, all without blocking the HTML parser.

Conclusion

Understanding when and how to use async and defer attributes can significantly enhance the performance of web applications by optimizing script loading behavior. By selecting the appropriate loading mechanism, developers can ensure both faster page loads and proper functionality of web applications.


Course illustration
Course illustration

All Rights Reserved.