Search code examples
javascriptwindowmicrosoft-edgereadystateonreadystatechange

What does ‘readystatechange’ event for ‘window’ mean?


Trying to handle window.onreadystatechange, I notice this event firing two times during page load. But I cannot figure out what exactly becomes changed after each event. If it were not for window, but for document, then there had been document.readyState property containing the current state. But in case of window, there isn’t any “window.readyState” or similar property. So what does it really mean when a readystatechange event for window fires, and what the difference between the first and the second firing is?

Here is my code that gives two seemingly identical console outputs:

'use strict';

window.addEventListener('readystatechange', function(e) {
  console.log(window, e);
});


Solution

  • window only fires the readystatechange event in IE and Edge (tested in IE 11). It does NOT fire in Firefox or Chrome.

    It is actually fired by the document, when its readyState changes to "interactive" and "complete" (bubbling).

    Thus, in IE:

    window.onreadystatechange == document.onreadystatechange
    

    I would not recommend using it though, as this event is not fired in the other browsers.