Skip to content Skip to sidebar Skip to footer

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? $(doc

Solution 1:

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() { });

Solution 2:

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.

Post a Comment for "How To Know The First Ready State Of Dom"