HTML Script Loading with defer, async, and type=module
Lesson 11 of 36 in the HTML5 learning path.
How do I load JavaScript without blocking HTML parsing?
A classic script with no defer and no async is parser-blocking. The browser stops building HTML until that file downloads and runs. defer downloads during parsing and then runs those scripts in source order after the document is parsed, before DOMContentLoaded. async also downloads during parsing, then runs each file as soon as it is ready, so two async scripts can finish in either order. A script with type="module" is a module, and module scripts are deferred unless you also set async.
Use defer for page code that needs the DOM and a stable order. Use async for an independent file that must not hold the others. Use type="module" when the program is written as modules. Keep a blocking script only for a tiny snippet that has to run at that exact spot in the file. The lab page uses all four shapes so you can see the order in one list.
Syntax & Parameters
<script src="app.js" defer></script> <script src="beacon.js" async></script> <script type="module" src="main.js"></script>
Example
Interactive preview available<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Script loading lab</title>
<style>
body { margin: 0; font: 1.05rem/1.55 Georgia, "Times New Roman", serif; color: #1c1917; background: #faf7f2; }
header { background: #1e3a5f; color: #f8fafc; padding: 1.25rem 1.5rem; }
main { max-width: 42rem; margin: 0 auto; padding: 1.25rem 1.5rem 2rem; }
h1 { font-size: 2rem; line-height: 1.2; margin: 0 0 0.75rem; }
h2 { font-size: 1.25rem; margin: 1.3rem 0 0.4rem; }
p, li { margin: 0 0 0.7rem; }
</style>
</head>
<body>
<header>
<p>North Campus, Web Systems 1</p>
</header>
<main>
<h1>Script loading lab</h1>
<p>The list fills itself as each script runs. Defer scripts keep source order after parsing. The async line can move. The module runs after parsing even though it has no defer attribute.</p>
<h2>What each attribute promises</h2>
<ul>
<li>No defer and no async: the parser stops at that classic script.</li>
<li>defer: download without blocking, then run in source order.</li>
<li>async: download without blocking, then run when ready, in no promised order.</li>
<li>type=module: treated as deferred unless the async attribute is also set.</li>
</ul>
<ol id="trace"></ol>
</main>
<!-- defer and async apply to classic scripts with a src. Data URLs keep this demo in one file. -->
<script src="data:text/javascript,document.getElementById(%22trace%22).insertAdjacentHTML(%22beforeend%22,%22%3Cli%3EBlocking%20script%20ran%20while%20the%20parser%20was%20on%20this%20tag.%3C/li%3E%22)"></script>
<script defer src="data:text/javascript,document.getElementById(%22trace%22).insertAdjacentHTML(%22beforeend%22,%22%3Cli%3EDefer%20A%20ran%20after%20parsing,%20in%20source%20order.%3C/li%3E%22)"></script>
<script defer src="data:text/javascript,document.getElementById(%22trace%22).insertAdjacentHTML(%22beforeend%22,%22%3Cli%3EDefer%20B%20ran%20after%20defer%20A.%3C/li%3E%22)"></script>
<script async src="data:text/javascript,document.getElementById(%22trace%22).insertAdjacentHTML(%22beforeend%22,%22%3Cli%3EAsync%20script%20ran%20as%20soon%20as%20it%20was%20fetched.%3C/li%3E%22)"></script>
<script type="module">
document.getElementById("trace").insertAdjacentHTML("beforeend", "<li>Module script ran after parsing, with no defer attribute.</li>");
</script>
</body>
</html>
Expected Output
The page shows the heading Script loading lab, four promise bullets, and an ordered list the scripts fill. With these immediate data URLs the list usually reads: the blocking script ran while the parser was on its tag, the async script ran as soon as it was fetched, Defer A ran after parsing in source order, Defer B ran after Defer A, and the module ran after parsing with no defer attribute. Defer A remains before Defer B. The module line remains with the deferred group, not in the middle of parsing. If the async file were a slow network script, its line could appear after the deferred scripts instead. The static bullets stay on the page either way. The address bar does not change.
Line-by-Line Walkthrough
- The list with id trace is in the body before any script, so each script can append a line once it runs.
- The unordered list above it is ordinary HTML. It states the contract: blocking, defer order, async with no promised order, and modules deferred by default.
- The first script has a src and no defer or async, so it is parser-blocking and runs while the parser is on that tag.
- The next two scripts use defer. They download without stopping the parser and run later, Defer A before Defer B, because defer keeps source order.
- The async script downloads without stopping the parser and runs when it is ready. Nothing in the markup promises its slot relative to the deferred scripts.
- These classic defer and async scripts need a src. The data URLs exist so one HTML file can run the demo. A production page points src at real files.
- The last script is type="module" and has no defer attribute. Module scripts are deferred unless async is set, so it still waits until parsing finishes.
- The module runs after the deferred classic scripts that appear before it, because ordered deferred scripts follow document order.
- None of the deferred scripts call document.write. After parsing, that call would wipe the page instead of adding a line.
Open the file and read the numbered list. Defer A should stay above Defer B. The async line is the one that can move if you replace the data URL with a slow remote file.
Common Pitfalls & Security
Script order bugs show up as missing elements and as beacons that run too early. The attributes do not make an untrusted file safe. They only change when a file you already chose to run will start.
- A large classic script in the head with no defer or async blocks first paint until it finishes.
- async scripts do not wait for one another. Code that expects a specific async partner to have run already will race.
- defer does not apply to a classic script that has no src. The attribute is ignored and the inline script runs immediately.
- document.write inside a deferred or async script runs after the stream is closed and can replace the whole document.
- A module on another origin needs the right CORS headers or the import fails. Load third-party files only from sources you trust, because they can read the DOM once they run.
Best Practices & Modern Standards
HTML5 lets you keep parsing while scripts download. Default to defer for your own classic files, in the order they depend on each other, and put them in the head or at the end of the body. The order of defer scripts is the document order, not the order the bytes happened to arrive.
Use async only for a file that stands alone, such as an analytics beacon. Use type="module" for application code split into imports, and remember that a module is deferred unless you add async. Avoid parser-blocking scripts on the path to first paint. Do not point src at a file you have not reviewed.
Real-World Use
The course home, index.html, loads its studio code with defer so the header and the syllabus link parse before any widget runs. A separate visit counter is async because the page must not wait on it and nothing else imports it. The lab app itself is type="module", so it waits for the document and can import a helper without a blocking script tag in the middle of the article.
When a classmate adds a chart library, it gets defer and a place after the module only if the module does not depend on it. If the module must run first, the library tag stays above the module in the file. The async beacon stays independent so a slow counter cannot reorder the lab.
Frequently Asked Questions
Does type=module imply defer?
Yes. A module script is deferred by default, even when the defer attribute is absent. It runs after the document is parsed and keeps order with other deferred scripts. Add async if that module should run as soon as it is ready instead of waiting its turn.
Will two defer scripts always run in source order?
Yes. Deferred classic scripts run in document order after parsing, before DOMContentLoaded. Their download times can differ, and the later file still waits for the earlier one. Async scripts are the ones that ignore that queue.
Why did my async script run before the footer existed?
Async may run as soon as the file is fetched, which can be before the parser reaches the footer. Do not assume elements below the script tag exist. Wait for DOMContentLoaded, or use defer when the script needs the full body.
Can I put defer on an inline script with no src?
Not on a classic inline script. defer and async apply to classic scripts that have a src. An inline type="module" script is different: it is deferred because it is a module. A classic inline script in the body runs at once and blocks the parser at that spot.