JavaScript Document readystatechange Event

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Document event
Baseline Widely available

What You’ll Learn

The Document readystatechange event fires whenever document.readyState changes. Learn the loadinginteractivecomplete path, how it relates to DOMContentLoaded and load, and five try-it labs.

01

Kind

Document event

02

Type

Event

03

Cancelable

No

04

Bubbles

No

05

Read

document.readyState

06

Status

Baseline · Widely available

Introduction

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.

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.
  • Handlerdocument.onreadystatechange or addEventListener("readystatechange", ...).
  • States — commonly loading, interactive, complete.
  • Status — Baseline Widely available since July 2015 (MDN).

📝 Syntax

Use the event name with addEventListener, or set the handler property on document:

JavaScript
addEventListener("readystatechange", (event) => { });

onreadystatechange = (event) => { };

Event type

A generic Event. Not cancelable and does not bubble.

MDN-style log

JavaScript
document.addEventListener("readystatechange", (event) => {
  console.log("readystate: " + document.readyState);
});

document.addEventListener("DOMContentLoaded", (event) => {
  console.log("DOMContentLoaded");
});

window.addEventListener("load", (event) => {
  console.log("load");
});

readyState values

ValueMeaning (beginner view)
loadingDocument is still loading / parsing
interactiveDocument parsed; sub-resources may still load (DOM ready)
completeDocument and sub-resources finished loading

⚖️ readystatechange vs DOMContentLoaded vs load

TopicreadystatechangeDOMContentLoadedload (window)
MeaningreadyState changedHTML parsed / DOM readyPage + sub-resources done
How oftenMultiple times per loadOnce (typically)Once (typically)
Read state?Yes — document.readyStateUsually at interactiveUsually at complete
Handler propertyonreadystatechangeNoneonload on window
Cancelable / bubblesNo / NoNo (generic Event)See MDN Window load

⚡ Quick Reference

GoalCode / note
Listendocument.addEventListener("readystatechange", fn)
Handler propertydocument.onreadystatechange = fn
Current statedocument.readyState
Run only when completeInside handler: if (document.readyState === "complete")
DOM-only setupPrefer DOMContentLoaded
MDN statusBaseline Widely available (Jul 2015)

🔍 At a Glance

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

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.

JavaScript
const log = document.getElementById("log");

document.addEventListener("readystatechange", () => {
  log.textContent += "readystate: " + document.readyState + "\n";
});
Try It Yourself

How It Works

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

How It Works

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

How It Works

Calling the handler once up front covers late scripts that missed earlier transitions. The listener covers future changes during the same load.

Example 4 — Event Timeline (MDN Live Pattern)

Log readystatechange, DOMContentLoaded, and load together.

JavaScript
const log = document.getElementById("log");

document.addEventListener("readystatechange", () => {
  log.textContent += "readystate: " + document.readyState + "\n";
});

document.addEventListener("DOMContentLoaded", () => {
  log.textContent += "DOMContentLoaded\n";
});

window.addEventListener("load", () => {
  log.textContent += "load\n";
});

log.textContent += "script ran; readyState=" + document.readyState + "\n";
Try It Yourself

How It Works

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();
  });
}
Try It Yourself

How It Works

Checking the current state first avoids missing complete when the script runs late. For DOM-only work, interactive / DOMContentLoaded is usually enough and faster.

🚀 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.

🔧 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.

📝 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.
  • Related learning: readyState, DOMContentLoaded, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-supported (prefer modern browsers)
Legacy
readystatechange Excellent

Bottom line: Listen on document for readystatechange, read document.readyState in the handler, and prefer DOMContentLoaded when you only need the DOM.

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.

Continue with scroll, readyState, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read document.readyState inside the handler
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about readystatechange

readyState changed — read the property for loading / interactive / complete.

5
Core concepts
🔍 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.

Next: Document scroll

Learn the Document event that fires when the document view has been scrolled.

scroll →

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.

5 people found this page helpful