CKTIS / JavaScript
script defer — deferred script execution
script defer lets an external classic script download alongside HTML parsing. Execution happens after parsing finishes and before the DOMContentLoaded event.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
<script defer src="app.js"></script>script defer applies to external classic scripts in the document handled by the parser. Downloading does not stop HTML parsing. The script executes after parsing finishes; this does not mean JavaScript runs in the background.
These scripts execute in document order, before DOMContentLoaded. They can access elements written later in the HTML. Their downloading and execution can delay that event; loading every image is not required.
defer is a boolean attribute: its presence enables it. Writing defer="false" also enables it and is an invalid value; remove the attribute to disable it. It has no effect on inline classic scripts or module scripts. Modules without async already have deferred execution. When both async and defer are present, async takes precedence. Dynamically inserting a script does not provide these same rules merely by setting defer.
The example uses a data: URL in src, so it remains an external classic script despite needing no second file. Expect the two stated console lines and the paragraph text Parsed.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>defer demo</title>
<script defer src="data:text/javascript,document.getElementById('status').textContent%3D'Parsed'%3Bconsole.log('defer%3A%20Parsed')"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
console.log('DOMContentLoaded: ' + document.getElementById('status').textContent);
});
</script>
</head>
<body>
<p id="status">Waiting</p>
</body>
</html>Result
defer: Parsed DOMContentLoaded: Parsed
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.