CKTIS / JavaScript
Document.readyState — document loading state
Document.readyState describes the document's loading stage: loading, interactive, or complete. Checking this property helps initialize DOM-related code even when a script loads too late to receive DOMContentLoaded.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
document.readyStateDocument.readyState, read as document.readyState, describes the document's current loading state. It is a browser Web API, not an ECMAScript language feature. MDN classifies it as widely available across browsers since July 2015; a Node.js version alone does not establish support for this API.
The value "loading" means the HTML parser is still working. At "interactive", the document has been parsed, but dependent resources may still be loading. The document reaches "complete" once it has finished loading and no resources are still delaying the load event. The load event fires after this transition. Images with loading="lazy" may still be pending because they do not delay that event. A state change fires readystatechange on the document.
The "interactive" state does not mean DOMContentLoaded has already fired: classic scripts with defer and module scripts scheduled to run after parsing may still be waiting to run. Before firing the event, the browser runs those classic scripts and initiates evaluation of those modules. It does not wait for a module suspended at a top-level await to finish, nor does it wait for module scripts with async or for images. DOM readiness therefore does not mean the page is fully loaded.
The example waits for DOMContentLoaded only during "loading"; otherwise, it calls init() immediately. This prevents a late-running script from waiting for an event that has already passed. This pattern provides access to the parsed DOM, but does not guarantee that other defer or module scripts have finished. { once: true } removes the listener after its first invocation.
Examples
(() => {
function init() {
console.log("DOM parsed");
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init, { once: true });
} else {
init();
}
})();Result
DOM parsed
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.