国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
How async works
How defer works
When to use which
A quick comparison summary
Home Web Front-end H5 Tutorial Explain the `async` and `defer` attributes for scripts in HTML5.

Explain the `async` and `defer` attributes for scripts in HTML5.

Jul 13, 2025 am 03:06 AM
html5 Script

The difference between async and defer is the execution timing of the script. async allows scripts to be downloaded in parallel and executed immediately after downloading, without guaranteeing the execution order; defer executes scripts in order after HTML parsing is completed. Both avoid blocking HTML parsing. Using async is suitable for standalone scripts such as analyzing code; defer is suitable for scenarios where you need to access the DOM or rely on other scripts.

Explain the `async` and `defer` attributes for scripts in HTML5.

When you're adding JavaScript to an HTML page, using the async or defer attributes can make a real difference in how your page loads and performs. These two attributes control how the browser handles script loading and execution, especially during the parsing of the HTML document.

Explain the `async` and `defer` attributes for scripts in HTML5.

Here's what they do in short:

  • async makes the script load in parallel with HTML parsing and executes it as soon as it's downloaded — without waiting for the HTML parsing to finish.
  • defer also loads the script while HTML is being parsed, but it waits until the entire HTML document is parsed before running the script.

Let's go into more detail about when and why you'd use each one.

Explain the `async` and `defer` attributes for scripts in HTML5.

What happens without async or defer

By default, when the browser encounters a <script></script> tag (without any attribute), it stops parsing the HTML. It downloads and runs the script immediately. Only after the script finishes executing does the browser continue building the DOM.

This behavior can cause delays in rendering the page, especially if the script is large or take time to download. That's why async and defer were introduced — to avoid blocking the parser.

Explain the `async` and `defer` attributes for scripts in HTML5.

How async works

The async attribute tells the browser that the script doesn't depend on the rest of the page content or other scripts. So the browser can:

  • Download the script in the background while it continues parsing HTML
  • Run the script as soon as it finishes downloading
  • Stop HTML parsing just long enough to execute the script

Because of this behavior, async is best used for scripts that are completely independent — like analytics trackers or simple widgets that don't interact with the page content.

Example:

 <script src="analytics.js" async></script>

Important notes:

  • The order of execution isn't guaranteed for multiple async scripts
  • They run as soon as they're ready, not in the order they appear in the HTML

How defer works

With defer , the script also downloads in the background during HTML parsing. But instead of running right away, it waits until the entire HTML document has been parsed.

This means:

  • HTML parsing continues uninterrupted
  • Scripts are executed in the order they appear in the document
  • Execution happens after the DOM is fully built but before the DOMContentLoaded event

This is ideal for scripts that need to access or manipulate the DOM, or rely on other deferred scripts.

Example:

 <script src="main.js" defer></script>

Key points:

  • defer preserves execution order
  • It ensures the DOM is ready when the script runs
  • No need to manually wait for DOMContentLoaded inside the script

When to use which

Choosing between async and defer depends on what the script does and whether it relies on anything else.

Use async if:

  • The script doesn't depend on the DOM or other scripts
  • You want it to run as soon as possible after download
  • It's something like a tracking code or third-party widget

Use defer if:

  • The script needs to access or modify the DOM
  • It relies on other scripts (especially those earlier in the page)
  • You want to ensure execution happens in a specific order

If a script is small and critical, sometimes it's even better to inline it in a <script></script> block rather than load it externally — but that's another topic.


A quick comparison summary

Feature Regular Script async defer
Blocks HTML parsing? Yes No No
Executes in order? N/A No Yes
Waits for HTML? No Yes
Waits for other scripts? No Yes (in order)

So, basically, pick async when the script can run anytime, and defer when it needs to wait for the page. Both help improve performance by avoiding render-blocking JavaScript — and that makes for a better user experience.

That's it. Not too complicated once you get the hang of it.

The above is the detailed content of Explain the `async` and `defer` attributes for scripts in HTML5.. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

HTML5: The Building Blocks of the Modern Web (H5) HTML5: The Building Blocks of the Modern Web (H5) Apr 21, 2025 am 12:05 AM

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

HTML5: The Standard and its Impact on Web Development HTML5: The Standard and its Impact on Web Development Apr 27, 2025 am 12:12 AM

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

HTML5 and H5: Understanding the Common Usage HTML5 and H5: Understanding the Common Usage Apr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

The Connection Between H5 and HTML5: Similarities and Differences The Connection Between H5 and HTML5: Similarities and Differences Apr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Understanding H5: The Meaning and Significance Understanding H5: The Meaning and Significance May 11, 2025 am 12:19 AM

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

H5: Exploring the Latest Version of HTML H5: Exploring the Latest Version of HTML May 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

H5 Code: Writing Clean and Efficient HTML5 H5 Code: Writing Clean and Efficient HTML5 Apr 20, 2025 am 12:06 AM

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

HTML5: Limitations HTML5: Limitations May 09, 2025 pm 05:57 PM

HTML5hasseverallimitationsincludinglackofsupportforadvancedgraphics,basicformvalidation,cross-browsercompatibilityissues,performanceimpacts,andsecurityconcerns.1)Forcomplexgraphics,HTML5'scanvasisinsufficient,requiringlibrarieslikeWebGLorThree.js.2)I

See all articles