Defer

Comparing Behaviors

AttributeDownloadExecutionHTML ParsingMain Risk
NoneBlocksImmediatePausedSlow Initial Render
AsyncParallelASAPPaused when downloadedRace Conditions
DeferParallelAfter HTMLContinuesDelayed Functionality

Order of Execution: Async, Defer, and Both

Understanding the order of execution for scripts with different attributes is crucial for managing dependencies and ensuring proper functionality. Here's how it works:

  1. Regular scripts (no async or defer):

  2. Async scripts:

  3. Defer scripts:

  4. Scripts with both async and defer:

Example Execution Order:

Possible execution order:

  1. 1.js (blocks parsing)
  2. 3.js or 2.js (whichever downloads first)
  3. 2.js or 3.js (whichever downloads second)
  4. 4.js
  5. 5.js

Note that 2 and 3 could execute in any order or even before 1 if 1.js takes longer to download.

Best Practices

  1. Use async for independent scripts like analytics.
  2. Use defer for scripts that depend on DOM or other scripts.
  3. Place scripts in the with async or defer to start downloading early.
  4. For critical scripts, consider inline scripts in the .

Browser Support

Both async and defer are widely supported in modern browsers. For older browsers, consider using a script loader or placing scripts at the end of the .

","image":"http://www.luping.net/uploads/20241009/1728469445670659c5d48f7.jpg","datePublished":"2024-11-08T07:51:11+08:00","dateModified":"2024-11-08T07:51:11+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Mastering Script Tags: Using Async and Defer for Precise Script Control

Mastering Script Tags: Using Async and Defer for Precise Script Control

Published on 2024-11-08
Browse:461

Mastering Script Tags: Using Async and Defer for Precise Script Control

In the world of web development, optimizing page load times is crucial. Two powerful attributes of the

The Basics: How Scripts Load

By default, when a browser encounters a

  1. Pauses HTML parsing
  2. Downloads the script
  3. Executes the script
  4. Resumes HTML parsing

This process can slow down page rendering, especially for large scripts or slow connections. Additionally, it can lead to bugs if the script runs before certain HTML elements are fully loaded, which often happens if the script is not placed correctly in the document.

Async and Defer: A Double-Edged Sword

Async


  • What it does: Downloads the script asynchronously while HTML parsing continues.
  • When it executes: As soon as it's downloaded, pausing HTML parsing.
  • When it is used: Independent scripts that don't rely on other scripts or DOM content.
  • Caveat: Can execute out of order, potentially breaking dependencies.

Defer


  • What it does: Downloads the script while HTML parsing continues.
  • When it executes: After HTML parsing is complete, but before the DOMContentLoaded event.
  • When it is used: Scripts that rely on DOM content or need to execute in a specific order.
  • Caveat: May delay execution of critical functionality.

Comparing Behaviors

Attribute Download Execution HTML Parsing Main Risk
None Blocks Immediate Paused Slow Initial Render
Async Parallel ASAP Paused when downloaded Race Conditions
Defer Parallel After HTML Continues Delayed Functionality

Order of Execution: Async, Defer, and Both

Understanding the order of execution for scripts with different attributes is crucial for managing dependencies and ensuring proper functionality. Here's how it works:

  1. Regular scripts (no async or defer):

    • Execute in the order they appear in the document.
    • Block HTML parsing until they're downloaded and executed.
  2. Async scripts:

    • Download in parallel and execute as soon as they're available.
    • No guaranteed order of execution; they run as soon as they're downloaded.
    • May execute before the DOM is fully loaded.
  3. Defer scripts:

    • Download in parallel but execute only after HTML parsing is complete.
    • Execute in the order they appear in the document.
    • Run before the DOMContentLoaded event.
  4. Scripts with both async and defer:

    • The async attribute takes precedence in modern browsers.
    • In older browsers that don't support async, they fall back to defer behavior.

Example Execution Order:






Possible execution order:

  1. 1.js (blocks parsing)
  2. 3.js or 2.js (whichever downloads first)
  3. 2.js or 3.js (whichever downloads second)
  4. 4.js
  5. 5.js

Note that 2 and 3 could execute in any order or even before 1 if 1.js takes longer to download.

Best Practices

  1. Use async for independent scripts like analytics.
  2. Use defer for scripts that depend on DOM or other scripts.
  3. Place scripts in the with async or defer to start downloading early.
  4. For critical scripts, consider inline scripts in the .

Browser Support

Both async and defer are widely supported in modern browsers. For older browsers, consider using a script loader or placing scripts at the end of the

.
Release Statement This article is reproduced at: https://dev.to/umairian/mastering-script-tags-using-async-and-defer-for-precise-script-control-d9n?1 If there is any infringement, please contact [email protected] delete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3