Reputation: 37876
i am trying to get the first ready state of the DOM. the second, third, etc is not interesting me, but the first one. is there any trick to get the first ready state of DOM?
$(document).ready(function() {
// is it the first ready state?
});
Upvotes: 2
Views: 1489
Reputation: 11245
There are 4 readyState possible values:
To see it's value use this code:
document.onreadystatechange = function () {
if (document.readyState === YourChoice) {
// ...
}
}
I could not catch the uninitialized
readyState. (but why should I need it?)
If you need a listener for complete load of the DOM, use:
document.addEventListener('DOMContentLoaded', YourListener);
or
document.addEventListener('load', YourListener);
or even
window.onload = YourListener;
for jquery:
$(document).on("DOMContentLoaded", function() { });
or
$(document).on("load", function() { });
or
$(window).on("load", function() { });
Upvotes: 3
Reputation: 664444
Ah, you're using jQuery. Have a look at the docs: There is only one ready
event! I will never fire multiple times. Internally, this is even handled with a Promise, so it cannot fire multiple times.
Upvotes: 1