Document.readyState
Document.readyState
속성을 통해 document
의 로딩 상태를 확인할 수 있다.
Document.readyState 속성 값이 바뀔 때 readystatechange
이벤트가 document
에서 일어난다.
Syntax
js
var string = document.readyState;
Values
Examples
Different states of readiness
js
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;
}
readystatechange as an alternative to DOMContentLoaded event
js
// Alternative to DOMContentLoaded event
document.onreadystatechange = function () {
if (document.readyState === "interactive") {
initApplication();
}
};
readystatechange as an alternative to load event
js
// Alternative to load event
document.onreadystatechange = function () {
if (document.readyState === "complete") {
initApplication();
}
};
readystatechange as event listener to insert or modify the DOM before DOMContentLoaded
js
document.addEventListener("readystatechange", (event) => {
if (event.target.readyState === "interactive") {
initLoader();
} else if (event.target.readyState === "complete") {
initApp();
}
});
명세서
Specification |
---|
HTML Standard # current-document-readiness |
브라우저 호환성
BCD tables only load in the browser
같이 보기
- 관련 이벤트