Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

defer: run classic scripts after parsing

Preserve document order without blocking HTML parsing during the download.

Version: HTML Living Standard

Syntax

<script defer src="/first.js"></script>

For parser-inserted external classic scripts, boolean defer allows parallel downloading and delays execution until HTML parsing finishes. These deferred scripts execute in document order before DOMContentLoaded. The attribute does not make an inline classic script deferred. Modules already defer by default and are not changed by this attribute.

If async is also present, its behavior takes priority in modern browsers. Deferred does not mean idle-time or background-thread execution: a long script can still block interaction. Do not apply the parser-inserted ordering rule blindly to dynamically created scripts.

The fixture deliberately delivers the second file before the first. Both still execute in document order, the first finds an element near the document’s end, and DOMContentLoaded comes last. The actual request and execution sequence is verified with local responses.

Examples

<!doctype html><html><head><meta charset="utf-8">
<script>window.order = []; document.addEventListener('DOMContentLoaded', () => { window.order.push('DOMContentLoaded'); document.querySelector('#result').textContent = window.order.join(','); });</script>
<!-- Local fixture: first.js is held until #tail exists; second.js arrives earlier. -->
<script defer src="/first.js"></script><script defer src="/second.js"></script>
</head><body><p id="tail">Parsed content</p><output id="result"></output></body></html>

Result

first:true,second,DOMContentLoaded

Only the first response is deliberately held; the recorded execution order is produced by the browser.

Sources