The Document.readyState property of a document
describes the loading state of the document.
The readyState of a document can be one of following:
document
is still loading.load
event is about to fire.When the value of this property changes a readystatechange
event fires on the document
object.
var string = document.readyState;
switch (document.readyState) { case "loading": // The document is still loading. break; case "interactive": // The document has finished loading. We can now access the DOM elements. // But sub-resources such as images, stylesheets and frames are still loading. var span = document.createElement("span"); span.textContent = "A <span> element."; document.body.appendChild(span); break; case "complete": // The page is fully loaded. console.log("The first CSS rule is: " + document.styleSheets[0].cssRules[0].cssText); break; }
// alternative to DOMContentLoaded event document.onreadystatechange = function () { if (document.readyState === "interactive") { initApplication(); } }
// alternative to load event document.onreadystatechange = function () { if (document.readyState === "complete") { initApplication(); } }
document.addEventListener('readystatechange', event => { if (event.target.readyState === "interactive") { initLoader(); } else if (event.target.readyState === "complete") { initApp(); } });
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Document readiness' in that specification. | Living Standard | |
HTML 5.1 The definition of 'Document readiness' in that specification. | Recommendation | |
HTML5 The definition of 'Document readiness' in that specification. | Recommendation | Initial specification. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | Yes | 4 | 9
|
11
|
5 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 4 | 11
|
5 | ? |
readystatechange
eventDOMContentLoaded
eventload
event
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/document/readyState