In JavaScript, the Document DOMContentLoaded event fires when the HTML has been completely parsed and deferred scripts (defer / type="module") have downloaded and executed (MDN). It does not wait for images, subframes, or async scripts.
Register a listener with addEventListener. Check readiness and browser support before you rely on this event.
Syntax
Listen for DOMContentLoaded. Start with this line.
JavaScript
document.addEventListener("DOMContentLoaded", fn)
Call
What it does
document.addEventListener("DOMContentLoaded", fn)
Primary signature.
Built into the browser. Listen with document.addEventListener("DOMContentLoaded", …) — no import required.
Library declarations
JavaScript
document.addEventListener("DOMContentLoaded", fn)
Parameters
Parameter
Description
addEventListener
Required. Only supported registration style (MDN)
readyState guard
Optional. Handles scripts that miss the event
Return Value
Return Value
Description
*
Fires as a Document event (listener receives the Event object).
JavaScript Examples
Example 1 — Basic DOMContentLoaded Listener
MDN style: wait until the HTML is parsed, then query the DOM.
JavaScript
document.addEventListener("DOMContentLoaded", (event) => {
console.log("DOM fully loaded and parsed");
const title = document.querySelector("h1");
console.log("Found h1:", title ? title.textContent : "(none)");
});
DOM loaded
readyState now: loading
/* or interactive/complete if already past parse */
How It Works
Define setup once.doSomething is the shared DOM-ready work.
Still loading? Attach a DOMContentLoaded listener.
Already past? Call doSomething() immediately.
No race. MDN: JavaScript run-to-completion means the document cannot flip between the check and addEventListener.
Example 3 — DOMContentLoaded vs load
Use the right event: DOM ready vs full page ready (MDN).
JavaScript
document.addEventListener("DOMContentLoaded", () => {
console.log("DOMContentLoaded — DOM ready");
});
window.addEventListener("load", () => {
console.log("load — full page (images, etc.)");
});
console.log("Listeners attached");
console.log("Prefer DOMContentLoaded when you only need the DOM");
Pick intentionally. MDN: it is a common mistake to use load when DOMContentLoaded is enough.
Compatibility
Browser Support
DocumentDOMContentLoaded is Baseline Widely available (MDN, since July 2015). It is a standard Document event — not Deprecated, Experimental, or Non-standard.
✓ Baseline · Widely available
Document DOMContentLoaded
Fires when the HTML is fully parsed (before images). Use addEventListener — no onDOMContentLoaded property.
99%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
DOMContentLoadedExcellent
Bottom line: Safe for virtually all browsers students use today. Prefer it over window load when you only need the DOM, and guard late scripts with document.readyState.
Frequently Asked Questions
It fires on Document when the HTML has been completely parsed and deferred scripts (defer and type="module") have downloaded and executed. It does not wait for images, subframes, or async scripts (MDN).
No. MDN marks it Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. MDN notes there is no onDOMContentLoaded event handler property. Use document.addEventListener("DOMContentLoaded", handler).
DOMContentLoaded means the DOM is ready. The window load event waits for the full page including images and other sub-resources. Prefer DOMContentLoaded when you only need the DOM (MDN).
Check document.readyState. If it is still "loading", add a DOMContentLoaded listener; otherwise run your setup immediately. This matters for async scripts, dynamic imports, and injected scripts (MDN).
No. It is a generic Event and is not cancelable (MDN).