CompitoAdd three scripts to the head: /analytics.js as async, /utils.js as defer, /app.js as defer. Order matters for defer scripts.
differimento vs asincrono (l'attributo di script corretto)
75 XP7 min
Teoria
When to choose which
| Attribute | Download | Execute | Order preserved | Use for |
| (none) | blocking | immediately | yes | almost never |
async | parallel | as soon as downloaded | NO | analytics, ads, isolated 3rd-party |
defer | parallel | after HTML parsed | YES | your 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.
🔒
Iscriviti per iniziare a programmare
La teoria è aperta a tutti. Editor, anteprima e check si sbloccano con una prova gratuita di 7 giorni — carta richiesta, annullabile in qualsiasi momento.
Iscriviti — prova gratuita →Le prime 10 lezioni di ogni percorso sono gratuite. Nessuna carta richiesta.