2

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?
});
doniyor
  • 36,596
  • 57
  • 175
  • 260
  • `$(window).load( function() { });` – Muhammad Talha Akbar Dec 13 '12 at 11:44
  • Handle the [`readystatechange` event](https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/readystatechange) yourself. jQuery's document ready handler tells you when the document is "ready", so your callback function will be called once, not on each ready state change. – nnnnnn Dec 13 '12 at 11:45
  • _document.onreadystatechange = function () { alert(document.readyState); }​_ – Anujith Dec 13 '12 at 11:46
  • How many do you expect? Seems there are only [two `readystate`s](https://developer.mozilla.org/en-US/docs/DOM/document.readyState), and [one `readystatechange`](https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/readystatechange). – Bergi Dec 13 '12 at 11:46
  • @nnnnnn i think it is same as `document.DOMContentLoaded` or JQUERY `$(document).ready()` – Muhammad Talha Akbar Dec 13 '12 at 11:48
  • @Bergi - There are three states (according to the page you linked to). – nnnnnn Dec 13 '12 at 11:48
  • so, there are 2 states: ``complete`` and ``interactive`` . what is ``interactive`` again? – doniyor Dec 13 '12 at 11:49
  • No, there are 3 states, and the page Bergi linked to explains what all three are. jQuery's document ready should fire when the state changes to `"interactive"`, _but_ note jQuery has extra code to simulate this in browsers that don't support it directly. – nnnnnn Dec 13 '12 at 11:50
  • i think `interactive` is when document is partially loaded and `complete` is when document is loaded as a whole! – Muhammad Talha Akbar Dec 13 '12 at 11:50

2 Answers2

3

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() { });
micnic
  • 10,915
  • 5
  • 44
  • 55
  • so, man moral of the story is that the first readystate will be considered `loading`? – Muhammad Talha Akbar Dec 13 '12 at 11:53
  • 2
    _"I could not catch the uninitialized readyState."_ - I would expect it starts in that state, so there wouldn't be an associated change event. _"but why should I need it?"_ - Indeed. – nnnnnn Dec 13 '12 at 11:55
  • 1
    @AspiringAqib usually yes, I guess that for iFrames it is possible to catch the `uninitialized` event – micnic Dec 13 '12 at 11:56
1

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375