Zum Hauptinhalt springen
← 📄 HTML & the platform·Modul A7 · Lektion 2
AufgabeAdd three scripts to the head: /analytics.js as async, /utils.js as defer, /app.js as defer. Order matters for defer scripts.

defer vs async (das richtige Skriptattribut)

75 XP7 min
Theorie

When to choose which

AttributeDownloadExecuteOrder preservedUse for
(none)blockingimmediatelyyesalmost never
asyncparallelas soon as downloadedNOanalytics, ads, isolated 3rd-party
deferparallelafter HTML parsedYESyour app code, dependency chains
<!-- BAD — blocks page render -->
<script src="/analytics.js"></script>

<!-- GOOD — analytics doesn't depend on anything -->
<script src="/analytics.js" async></script>

<!-- GOOD — your app code, runs in source order -->
<script src="/utils.js" defer></script>
<script src="/components.js" defer></script>
<script src="/app.js" defer></script>

Why "order preserved" matters

defer scripts run in source order — utils.js finishes before app.js starts. async runs whichever finishes downloading first — app.js might execute before utils.js, breaking your dependency chain.

ES modules are deferred by default

<script type="module" src="/main.js"></script>

type="module" implies defer. No need to add it. Modules also automatically run in source order.

Inline scripts

async/defer only work on EXTERNAL scripts (src="..."). Inline <script> blocks always run synchronously when parsed.

🔒

Anmelden, um zu programmieren

Theorie ist für alle offen. Editor, Live-Preview und Check werden mit 7-Tage-Gratis-Test freigeschaltet — Karte erforderlich, jederzeit kündbar.

Registrieren — Gratis-Test →

Die ersten 10 Lektionen jedes Tracks sind kostenlos. Keine Karte nötig.

← ZurückNächste Lektion →

Bekomme einen Python- oder Web-Tipp pro Tag — per E-Mail

Kurz, handgeschrieben, kein Spam. Abmelden mit einem Klick.