🔧 AI Nachrichten Debian is Voting on Whether to Allow AI-Assisted Contributions(23.08.2026 um 09:34 Uhr)
🔧 AI Nachrichten The Linux Kernel Is Approaching 2,000 CVEs Per Release(29.08.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenCitrix Adds a Linux-Powered Escape Hatch For Compromised Windows PCs(30.08.2026 um 17:34 Uhr)
🔧 AI Nachrichten Debian is Voting on Whether to Allow AI-Assisted Contributions(23.08.2026 um 09:34 Uhr)
🔧 AI Nachrichten The Linux Kernel Is Approaching 2,000 CVEs Per Release(29.08.2026 um 20:00 Uhr)
⚠️ Malware / Trojaner / VirenCitrix Adds a Linux-Powered Escape Hatch For Compromised Windows PCs(30.08.2026 um 17:34 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 2 Min Lesezeit
0

When to use "async" and when "defer" in script tag?

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

The async and defer attributes in the <script> tag are used to optimize script loading and execution in HTML. Both attributes allow scripts to load without blocking the rendering of the page, but they have key differences in how they handle execution.









1. async Attribute





  • How it Works:




    • The script is loaded asynchronously, meaning it downloads in parallel with other resources (like CSS or images).

    • The script executes as soon as it is downloaded, which may happen before, during, or after the HTML parsing is complete.








  • Best for:




    • Independent scripts that do not rely on or interact with other scripts.

    • Examples:

    • Analytics scripts (e.g., Google Analytics).

    • Advertising or tracking scripts.

    • These scripts do not depend on the HTML or other scripts to run correctly.






  • Example:







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








  • Use Case:
    When script execution timing does not need to follow the document parsing order or depend on other scripts.









2. defer Attribute





  • How it Works:




    • The script is downloaded in parallel with other resources, just like async.

    • The script is executed only after the entire HTML document has been parsed.








  • Best for:




    • Scripts that depend on the complete DOM structure being available.

    • Scripts that interact with or depend on other scripts in a specific order.

    • Examples:

    • Frameworks or libraries (e.g., jQuery, Angular).

    • Inline scripts that manipulate DOM elements.






  • Example:







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








  • Use Case:
    When the script should execute only after the HTML document is fully parsed and you need to maintain execution order among multiple scripts.









Key Differences Between async and defer


























Attribute Script Loading Script Execution Order Guarantee
async Parallel to HTML parsing As soon as it’s downloaded No, execution is immediate
defer Parallel to HTML parsing After HTML parsing is complete Yes, scripts execute in order








Summary





  • Use async for:




    • Independent scripts that don’t depend on other scripts or the DOM.

    • Performance-critical tasks like analytics or advertising.








  • Use defer for:




    • Scripts that rely on the DOM being fully loaded.

    • Multiple scripts that must execute in a specific order.






Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Debian is Voting on Whether to Allow AI-Assisted Contributions
1 Quelle
The Linux Kernel Is Approaching 2,000 CVEs Per Release
1 Quelle
Citrix Adds a Linux-Powered Escape Hatch For Compromised Windows PCs
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten When to use "async" and when "defer" in script tag?

Thematisch verwandte Begriffe: When, async, when, defer · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...