HTML defer Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

HTML Syntax

syntax.html
<script src="file.js"  defer></script>

Boolean form: write the name alone. defer="" or defer="defer" means the same thing.

Attribute Values

defer is boolean — presence turns the behavior on:

ValueDescriptionTry it
defer (present)Fetch in parallel; run after parse, in document orderTry it
(absent)Default classic script: parser may block until the script loads and runsTry it
vs asyncasync runs ASAP and does not preserve orderTry it
multiple deferSeveral deferred scripts run in the order they appear in the HTMLTry it

Live Preview

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

    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>
    Try It Yourself

    How It Works

    1. src points at an external script (required for classic defer).
    2. defer starts the download without pausing the parser for the fetch.
    3. After parse, the script runs — safe to use getElementById on elements below.
    4. 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>
    Try It Yourself

    How It Works

    1. Both avoid blocking the HTML download while fetching.
    2. defer waits until parsing finishes, then runs in order.
    3. async executes whenever the file arrives.
    4. Pick defer for app code that touches the DOM or depends on earlier scripts.

    Example 3 — Several defer Scripts (Ordered)

    Deferred scripts keep document order — safe for library → app chains.

    HTML
    <script defer src="jquery.js"></script>
    <script defer src="plugin.js"></script>
    <script defer src="app.js"></script>
    
    <!-- Risky with async: plugin may run before jquery -->
    <!-- <script async src="jquery.js"></script> -->
    <!-- <script async src="plugin.js"></script> -->
    Try It Yourself

    How It Works

    1. List scripts in the order they must run.
    2. Add defer to each external classic script.
    3. Browser keeps that order when executing after parse.
    4. 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.

    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 Chrome All modern
    Full support
    Mozilla Firefox All modern
    Full support
    Apple Safari All modern
    Full support
    Microsoft Edge All modern
    Full support
    Opera All modern
    Full support
    Samsung Internet Modern
    Full support
    Internet Explorer 10+
    Supported
    defer Widely 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.

    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.

    Next: dir

    Set text direction with the dir global attribute (ltr, rtl, auto).

    dir tutorial →

    About the author

    Mari Selvan M P
    Mari Selvan M P 🔗

    Developer, cloud engineer, and technical writer

    • Experience 12 years building web and cloud systems
    • Focus Full Stack Development, AWS, and Developer Education

    I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

    8 people found this page helpful