As a page loads, the Document moves through a small set of loading states stored in document.readyState. Each time that value changes, the browser fires readystatechange on the Document.
That makes the event a progress signal: “the document is still loading,” “the DOM is interactive,” or “loading is complete.” You still read document.readyState inside the handler—the event itself does not carry the new state as a separate property.
💡
Beginner tip
For “DOM is ready,” many apps use DOMContentLoaded. Use readystatechange when you want every state transition, or when you already switch on document.readyState.
Concept
Understanding readystatechange
A standard Document event that answers: “Did document.readyState just change?”
Fires when the Document readyState attribute changes (MDN).
Not cancelable and does not bubble — listen on document.
Event type — a generic Event.
Handler — document.onreadystatechange or addEventListener("readystatechange", ...).
States — commonly loading, interactive, complete.
Status — Baseline Widely available since July 2015 (MDN).
Foundation
📝 Syntax
Use the event name with addEventListener, or set the handler property on document:
Four facts to remember about Document readystatechange.
Event type
Event
Plain Event
Means
State changed
Check readyState
Path
l → i → c
loading → complete
Baseline
yes
Widely available
Hands-On
Examples Gallery
Examples follow MDN Document: readystatechange event. In try-it labs the document is often already complete, so you may see the current state immediately—reload or log early to catch earlier transitions.
📚 Getting Started
Log every readyState change and use the handler property.
Example 1 — Log readyState (MDN style)
Append each new state when readystatechange fires.
MDN’s live example logs document.readyState on each readystatechange. Exact lines depend on when your listener was attached—early scripts can see more transitions.
Example 2 — document.onreadystatechange
Update a status line with the handler property.
JavaScript
const out = document.getElementById("out");
document.onreadystatechange = () => {
out.textContent = "readyState is now: " + document.readyState;
};
// Also show the current value immediately
out.textContent = "readyState is now: " + document.readyState;
Unlike DOMContentLoaded, onreadystatechange exists. Prefer addEventListener when you need more than one listener.
📈 Switch, Timeline & Complete
Branch on each state, compare related events, and gate work until complete.
Example 3 — switch (document.readyState)
Run different setup code for interactive vs complete (classic MDN readyState pattern).
JavaScript
const out = document.getElementById("out");
function handleReadyState() {
switch (document.readyState) {
case "loading":
out.textContent = "Still loading…";
break;
case "interactive":
out.textContent = "DOM interactive — safe to query elements";
break;
case "complete":
out.textContent = "Complete — images and other resources finished";
break;
}
}
document.addEventListener("readystatechange", handleReadyState);
handleReadyState(); // handle current state if event already passed
MDN’s demo pairs these events so beginners can see the order. Exact order can vary with when the script runs; early inline scripts show the richest timeline.
Example 5 — Run Only When complete
Gate heavy work until the document reports a fully loaded state.
JavaScript
const out = document.getElementById("out");
function onComplete() {
out.textContent = "Page complete — safe for full-resource work";
}
if (document.readyState === "complete") {
onComplete();
} else {
document.addEventListener("readystatechange", () => {
if (document.readyState === "complete") onComplete();
});
}
Checking the current state first avoids missing complete when the script runs late. For DOM-only work, interactive / DOMContentLoaded is usually enough and faster.
Applications
🚀 Common Use Cases
Logging or debugging document load progress.
Switching UI copy as the page moves to interactive / complete.
Running different init steps per readyState value.
Guarding late scripts so they still run after the document is ready.
Teaching how DOMContentLoaded and load fit the state path.
Under the Hood
🔧 How It Works
1
Document starts loading
readyState is loading; parsing is underway.
loading
2
Becomes interactive
readystatechange fires; DOM is typically ready to query.
interactive
3
DOMContentLoaded
Often around the interactive stage—HTML parsed, deferred scripts run.
DOM ready
4
✓
complete + load
Another readystatechange, then window load when resources finish.
Important
📝 Notes
Baseline Widely available (since July 2015)—no Deprecated / Experimental / Non-standard banner.
Not cancelable and does not bubble—listen on document.
Always read document.readyState in the handler for the new value.
Late scripts may miss early transitions—check the current state once on startup.
Document readystatechange is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project. It is a standard Document event used across modern browsers.
✓ Baseline · Widely available
Document readystatechange
Fires when document.readyState changes (loading, interactive, complete). Pair with DOMContentLoaded and load to understand page readiness.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-supported (prefer modern browsers)
Legacy
readystatechangeExcellent
Bottom line: Listen on document for readystatechange, read document.readyState in the handler, and prefer DOMContentLoaded when you only need the DOM.
Wrap Up
Conclusion
readystatechange is the Document event for every readyState transition. Use it with a switch when you care about loading progress; use DOMContentLoaded when you only need a DOM-ready signal; use load when you need full resources.
Handle the current state once if your script may run late
Use DOMContentLoaded for simple DOM setup
Prefer addEventListener for multiple listeners
Log alongside load when teaching the timeline
❌ Don’t
Expect the Event object itself to contain the new state string
Assume one readystatechange equals “DOM ready”
Wait for complete when interactive is enough
Forget late scripts may miss early transitions
Treat this as Experimental—it is Baseline Widely available
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about readystatechange
readyState changed — read the property for loading / interactive / complete.
5
Core concepts
📄01
State changed
readyState updated
Event
🔍02
Read property
document.readyState
API
🔄03
Three values
loading → complete
Path
🚫04
No bubble
listen on document
DOM
✅05
Baseline
widely available
Compat
❓ Frequently Asked Questions
It fires when the Document readyState attribute has changed. Typical values are loading, interactive, and complete. Read document.readyState inside the handler to see the new state.
No. MDN marks Document readystatechange as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
No. MDN states the event is not cancelable and does not bubble. Listen on document.
As the document loads, readyState moves through loading → interactive → complete. DOMContentLoaded is closely tied to the interactive stage; the window load event is closely tied to complete. MDN’s live example logs all three together.
Yes. You can use document.onreadystatechange or document.addEventListener("readystatechange", ...).
Use DOMContentLoaded when you only care that the DOM is ready. Use readystatechange when you want to react to every readyState transition, or when you already check readyState in a switch.
Did you know?
Unlike DOMContentLoaded, which has no onDOMContentLoaded property, Document supports onreadystatechange. That historic handler is still common in older tutorials alongside document.readyState checks.