我试图找出等待文档加载的最佳方法。
我想做什么:
根据我的理解,我会做这样的事情:
window.location = "https://somewebsite.com"
function isLoaded() {
while (document.readyState != "complete") {
}
return true;
}
isLoaded()
问题:
答案 0 :(得分:0)
三个选项:
<强>的onreadystatechange 强>
document.onreadystatechange = function () {
if (document.readyState == "complete") {
// document is ready. Do your stuff here
}
}
<强> DOMContentLoaded 强>
document.addEventListener('DOMContentLoaded', function() {
console.log('document is ready. I can sleep now');
});