doniyor
doniyor

Reputation: 37876

how to know the first ready state of DOM

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

Answers (2)

micnic
micnic

Reputation: 11245

There are 4 readyState possible values:

  • uninitialized - Has not started loading yet
  • loading - Is loading
  • interactive - Has loaded enough and the user can interact with it
  • complete - Fully loaded

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

Bergi
Bergi

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

Related Questions