The defer attribute on a <script> with src fetches the file in parallel with HTML parsing, then runs it after the document is parsed — before DOMContentLoaded — in document order.
It is a boolean attribute. Prefer defer for app scripts that need the DOM or depend on earlier scripts. Use async for independent scripts (analytics, ads). Modules (type="module") already defer by default.
Baseline · Widely available.
Not deprecated. Stable across modern browsers since about July 2015.
Needs src (classic scripts).
On inline classic scripts, defer has no effect. If both async and defer are set, async wins.
Syntax
HTML Syntax
syntax.html
<script src="file.js" defer></script>
Boolean form: write the name alone. defer="" or defer="defer" means the same thing.
Values
Attribute Values
defer is boolean — presence turns the behavior on:
Value
Description
Try it
defer (present)
Fetch in parallel; run after parse, in document order
Compare defer, async, and a blocking script with a short simulated timeline.
Educational timeline — real network timing can still vary, especially with async.
Event log
Mode: defer
Code
HTML Examples
Short, copy-ready snippets. Use Try It Yourself for a live editor.
Example 1 — External Script with defer
Download while parsing; run after the HTML is ready so the DOM exists.
HTML
<h1>Hello</h1>
<p id="status">Waiting for deferred script…</p>
<script defer src="data:application/javascript,document.getElementById('status').textContent='Deferred script ran after HTML was parsed.'"></script>
<p>This paragraph is parsed before the deferred script runs.</p>
This paragraph is parsed before the deferred script runs.
How It Works
src points at an external script (required for classic defer).
defer starts the download without pausing the parser for the fetch.
After parse, the script runs — safe to use getElementById on elements below.
DOMContentLoaded waits until deferred scripts finish evaluating.
Example 2 — defer vs async
Same parallel download idea — different run time and ordering rules.
HTML
<!-- Needs DOM / order: run after parse, in document order -->
<script defer src="app.js"></script>
<script defer src="widgets.js"></script>
<!-- Independent: run ASAP, order not guaranteed -->
<script async src="analytics.js"></script>
Browser keeps that order when executing after parse.
Do not mix async into the same dependency chain.
Edge Cases & Pitfalls
Most surprises come from inline scripts, modules, or combining async with defer.
src
Ignored without src
Classic inline scripts ignore defer. Put the code in an external file if you need deferred loading.
both
async + defer → async wins
Spec: if both are present, the element acts as if only async is set.
modules
Modules already defer
type="module" is deferred by default. The defer attribute has no effect on modules.
events
Delays DOMContentLoaded
The event waits until deferred scripts have loaded and finished evaluating — plan listeners accordingly.
placement
Fine in <head>
With defer, scripts in the head still run after the body is parsed. You do not need to move them to the bottom for DOM safety.
Compatibility
Browser Support
defer is an HTML attribute on <script> in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
defer
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support defer on external classic scripts.
97%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer10+
Supported
deferWidely supported
Bottom line: Safe default for most page scripts that need the DOM. Use async only when scripts are independent and order does not matter.
Remember
Key Takeaways
Role: fetch external scripts in parallel; run after HTML parse.
Order: multiple deferred scripts run in document order.
vs async: defer = after parse + ordered; async = ASAP + unordered.
Best for: DOM-dependent app code — not independent ads alone.
One line:defer loads scripts early and runs them after parse, in order — ideal when the DOM or script order matters.
Frequently Asked Questions
On an external script, defer downloads the file in parallel with HTML parsing, then runs it after the document is parsed (before DOMContentLoaded), keeping document order among deferred scripts.
The script element with a src attribute. On classic inline scripts without src, defer has no effect.
Both fetch in parallel. async runs as soon as the script is ready and does not preserve order. defer waits until parsing finishes and runs deferred scripts in document order.
Avoid it. If both are present, the element acts as if only async is set — defer is ignored.
Module scripts already defer by default. The defer attribute has no effect on modules.
Yes. Deferred scripts must load and finish evaluating before DOMContentLoaded fires.