Script Loading
Scripts can block HTML parsing depending on how they are included. defer downloads scripts while allowing parsing to continue and executes them after parsing. async downloads independently and executes as soon as available, so execution order should not be relied upon between async scripts.
Defer and Async
<script src="app.js" defer></script>\n<script src="analytics.js" async></script>💡 defer is often useful for application scripts that depend on the document being parsed. async is useful for independent scripts.
Key Takeaways
- defer preserves document parsing and executes after parsing.
- async scripts execute independently when ready.
Knowledge Check
Which attribute is generally appropriate for scripts that should execute after HTML parsing?