Reputation: 326
I am trying to figure out the best approach to wait for a document to load.
What I am trying to do:
From my understanding I would do something like this:
window.location = "https://somewebsite.com"
function isLoaded() {
while (document.readyState != "complete") {
}
return true;
}
isLoaded()
The problem:
Upvotes: 2
Views: 6747
Reputation: 410
Three options:
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');
});
Upvotes: 5