🕵️ SicherheitslückenCVE-2026-58728 | Google Android ARM64 MMU mmu.h ARM64_TLBI race condition(15.09.2026 um 21:40 Uhr)
🔧 ProgrammierungEnforce GitHub Advanced Security configurations(15.09.2026 um 21:31 Uhr)
🔧 ProgrammierungHow i give my coding agent a map of the repo with Empryo(15.09.2026 um 21:54 Uhr)
🔧 ProgrammierungReef Connects Agent Feedback, Learning and Versioned Delivery(15.09.2026 um 21:55 Uhr)
🔧 ProgrammierungUAJY Handbook RAG Chatbot Splits FAISS Search From Gemini(15.09.2026 um 21:55 Uhr)
🔧 ProgrammierungKnowledge Base For AI Agents: What It Must Do(15.09.2026 um 22:00 Uhr)
🕵️ SicherheitslückenCVE-2026-58728 | Google Android ARM64 MMU mmu.h ARM64_TLBI race condition(15.09.2026 um 21:40 Uhr)
🔧 ProgrammierungEnforce GitHub Advanced Security configurations(15.09.2026 um 21:31 Uhr)
🔧 ProgrammierungHow i give my coding agent a map of the repo with Empryo(15.09.2026 um 21:54 Uhr)
🔧 ProgrammierungReef Connects Agent Feedback, Learning and Versioned Delivery(15.09.2026 um 21:55 Uhr)
🔧 ProgrammierungUAJY Handbook RAG Chatbot Splits FAISS Search From Gemini(15.09.2026 um 21:55 Uhr)
🔧 ProgrammierungKnowledge Base For AI Agents: What It Must Do(15.09.2026 um 22:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Understanding async vs defer in the <script> Tag 🚀

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

When working with scripts in HTML, you may have noticed the async and defer attributes on the <script> tag. These attributes play a significant role in optimizing the loading and execution of JavaScript files.



Understanding their differences and when to use each can improve your website’s performance and user experience.



In this article, we’ll explore how async and defer work and provide practical examples to clarify their usage.









📌 The Problem with Default Script Loading



By default, when the browser encounters a <script> tag, it pauses parsing the HTML document to download and execute the script. This blocking behavior can delay the loading of the rest of the page, negatively affecting performance.



To address this, the async and defer attributes allow the non-blocking loading of scripts, enabling the browser to continue parsing HTML while the script is being downloaded.



However, these attributes differ in how they handle script execution.









📍 What is async?



The async attribute loads the script asynchronously. This means:




  • The script is downloaded in parallel with the HTML parsing.


  • Once the script is downloaded, it is executed immediately, potentially interrupting the HTML parsing process.










Key Characteristics of async 🔻



1- Execution Order: Scripts with async are executed as soon as they are ready, regardless of their order in the document.



2- Best for Independent Scripts: Use async for scripts that don’t rely on or interact with other scripts or the DOM (e.g., analytics or ads).









✨ Example of async






CODE
<!DOCTYPE html>
<html lang="en">
<head>
<title>Async Example</title>
</head>
<body>
<h1>Async vs Defer</h1>
<script async src="script1.js"></script>
<script async src="script2.js"></script>
</body>
</html>






In this example, script1.js and script2.js are downloaded and executed independently. Their execution order depends on which script finishes downloading first.












📍 What is defer?



The defer attribute ensures that the script is downloaded in parallel but delays execution until after the HTML document is fully parsed.









Key Characteristics of defer 🔻



1- Execution Order: Scripts with defer are executed in the order they appear in the document.



2- Best for Dependent Scripts: Use defer for scripts that manipulate the DOM or depend on other scripts.









✨ Example of defer






CODE
<!DOCTYPE html>
<html lang="en">
<head>
<title>Defer Example</title>
<script defer src="script1.js"></script>
<script defer src="script2.js"></script>
</head>
<body>
<h1>Async vs Defer</h1>
</body>
</html>






In this example, script1.js is guaranteed to execute before script2.js, even though both scripts are downloaded simultaneously.









Comparing async and defer 🚀


























Attribute Loading Behavior Execution Timing Use Case
async Downloads in parallel Executes as soon as ready Independent scripts (e.g., analytics)
defer Downloads in parallel Executes after HTML parsing DOM-dependent scripts or multi-scripts


Visualizing the Difference 💯




  • Without Attributes: Blocks HTML parsing until the script is downloaded and executed.


  • With async: HTML parsing continues during download but stops for execution.


  • With defer: HTML parsing and downloading happen simultaneously; execution waits until parsing is complete.










Practical Scenarios 🔻






Scenario 1: Loading Analytics



For analytics or tracking scripts that don’t interact with other scripts or the DOM:




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









Scenario 2: Loading Multiple Scripts with Dependencies



For scripts that rely on each other or manipulate the DOM:




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






Here, library.js will load and execute before main.js, ensuring proper dependency handling.












Key Takeaways ✅




  • Use async for independent scripts to load them quickly without waiting for HTML parsing.


  • Use defer for scripts that interact with the DOM or depend on other scripts.


  • Avoid using both async and defer on the same script as it may lead to unpredictable behavior.




By strategically using async and defer, you can optimize script loading, improve page speed, and enhance user experience.









🌐 Connect With Me On:



📍

📍

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
3 Quellen
CVE-2026-58728 | Google Android ARM64 MMU mmu.h ARM64_TLBI race condition
1 Quelle
DFN-CERT-2026-4853 Xcode: Eine Schwachstelle ermöglicht das Ausspähen von Informationen
1 Quelle
Enforce GitHub Advanced Security configurations
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Understanding async vs defer in the Tag 🚀

Thematisch verwandte Begriffe: Understanding, async, defer, ltscriptgt · 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 ...