使用 pure JavaScript 檢查文件是否準備
可以跨瀏覽器,而且使用 pure JavaScript 來確認文件是否已經載入完成的方式是使用 readyState
。
if (document.readyState === 'complete') {
// 網頁已經完全載入
}
當文件載入後,你可以檢查文件是否載入完成…
let stateCheck = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(stateCheck);
// 文件載入
}
}, 100);
或使用 onreadystatechange…
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
// 文件載入
}
};
當 DOM 載入後,使用 document.readyState === 'interactive'
來檢查是否載入完成。
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
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