DOMContentLoaded vs load
DOMContentLoadedfires ondocumentwhen the HTML has been parsed and the DOM is built, and deferred scripts have run. It doesn’t wait for images, fonts or iframes.loadfires onwindowwhen the page and all its resources (stylesheets, images, iframes) have finished loading.
DOMContentLoaded always comes first.
document.addEventListener('DOMContentLoaded', () => {
// Safe to query and change the DOM
});
window.addEventListener('load', () => {
// Images have dimensions, fonts and stylesheets are in
});
1. The timeline
HTML arrives
│ parser builds the DOM
│ ├─ <script> (no attribute): parser stops, fetches, runs, continues
│ ├─ <script defer>: fetched in parallel, runs after parsing, in order
│ └─ <script async>: fetched in parallel, runs whenever it arrives
▼
DOM complete → deferred scripts run → DOMContentLoaded
│ images, iframes, fonts still loading
▼
load
2. How scripts affect DOMContentLoaded
| Script type | Blocks parsing | Delays DOMContentLoaded |
|---|---|---|
<script src> |
Yes | Yes |
<script defer> |
No | Yes: it runs just before the event |
<script type="module"> |
No (deferred by default) | Yes |
<script async> |
Only while it executes | No: it may run before or after |
A stylesheet doesn’t block the parser, but a script that comes after a stylesheet waits for that CSS to load, so slow CSS can still push DOMContentLoaded back.
3. document.readyState
| Value | Meaning | Event |
|---|---|---|
'loading' |
Still parsing | |
'interactive' |
DOM built | DOMContentLoaded is about to fire |
'complete' |
Everything loaded | load is about to fire |
4. A safe ready() helper
If your code runs after the event has already fired (an async script, or a module loaded later), the listener never runs. Check the state first:
function ready(fn) {
if (document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn, { once: true });
}
}
ready(() => initApp());
This is the modern equivalent of jQuery’s $(document).ready(fn).
5. Do you still need these events?
Often not.
- With
<script defer>ortype="module", your code already runs after the DOM is parsed, so you can query elements straight away. - In React, Vue and similar frameworks, component lifecycle (
useEffect,onMounted) replaces them.
They still matter for third-party snippets that load async, for measuring performance, and for code that needs image dimensions (load).
6. Related events
pageshow: fires on every page display, including when the page is restored from the back/forward cache.loaddoesn’t fire again in that case, so usepageshowfor code that must run on every visit.beforeunload/pagehide: the page is going away. Preferpagehideorvisibilitychangefor sending analytics.
7. Performance angle
Both timings appear in DevTools’ Network panel (blue and red lines). They’re useful for debugging but they aren’t user-facing metrics. For that, look at LCP and INP in Core Web Vitals, and at the critical rendering path for what blocks first paint.
Choosing an event
| Need | Use |
|---|---|
| Attach handlers, read or change the DOM | DOMContentLoaded, or just a defer script |
| Measure images, wait for all assets | load |
| Run on back/forward cache restores too | pageshow |
| Unknown load timing (async script) | The ready() helper |
Follow-up questions to expect
- “
asyncvsdefer?” Both download in parallel.deferruns in document order after parsing;asyncruns as soon as it arrives, in any order. - “Where should scripts go?” In
<head>withdefer, so the download starts early without blocking the parser. - “Does
DOMContentLoadedwait for CSS?” Not by itself, but scripts after a stylesheet do, so in practice it often does.
