纯JS监听document是否加载完成
跨浏览器且纯JavaScript检测document是否加载完成的方法是使用readyState
.
if (document.readyState === 'complete') {
// 页面已完全加载
}
这样可以在document完全加载时监测到……
let stateCheck = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(stateCheck);
// document ready
}
}, 100);
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
// document ready
}
};
使用document.readyState === 'interactive'
监听DOM是否加载完成。
![jstips book](/assets/images/book.png)
MEET THE NEW JSTIPS BOOK
You no longer need 10+ years of experience to get your dream job.
Use the 100 answers in this short book to boost your confidence and skills to ace the interviews at your favorite companies like Twitter, Google and Netflix.
GET THE BOOK NOW![jstips amazon](/book/assets/bestseller.png)
![jstips book](/assets/images/book.png)
MEET THE NEW JSTIPS BOOK
The book to ace the JavaScript Interview.
A short book with 100 answers designed to boost your knowledge and help you ace the technical interview within a few days.
GET THE BOOK NOW![jstips amazon](/book/assets/bestseller.png)