What does ‘readystatechange’ event for ‘window’ mean?
Asked Answered
T

1

6

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);
});
Tulatulip answered 13/12, 2016 at 1:4 Comment(10)
"readyState" is indeed a property on the document object: developer.mozilla.org/en-US/docs/Web/API/Document/readyStateKemerovo
I don't get any events. What browser are you using? What other scripts are loaded on that page?Parmenides
@JeffMcCloud, I know a lot about readyState property and readystatechange event for document, but my question was about handling this event for window, not for document.Tulatulip
@Bergi, I’m using MS Edge 38.14393.0.0, and since it’s a test page, no other scripts are linked to the page.Tulatulip
Is it possible Edge somehow makes it bubble to the window ? Can you check if it corresponds to the document events ?Gunnysack
@Displayname Ah, cannot reproduce in another browser. Can you post which values you get for e?Parmenides
@Bergi, here it goes: { [functions]: , proto: { }, AT_TARGET: 2, bubbles: true, BUBBLING_PHASE: 3, cancelable: false, cancelBubble: false, CAPTURING_PHASE: 1, currentTarget: { }, defaultPrevented: false, eventPhase: 3, isTrusted: true, returnValue: true, srcElement: { }, target: { }, timeStamp: 1481592095815, type: "readystatechange" }Tulatulip
Interesting. I'll guess that __proto__ is Event.prototype and currentTarget is window. But what are target and srcElement, do they refer to document?Parmenides
Yes @Bergi, you’re right. target and srcElement both refer to document, while currentTarget is window.Tulatulip
And bubbles is true, which confirms @Kaiido's theory. Although it shouldn't. And really not from document to window. Looks like a bug to me.Parmenides
A
9

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.

Abreaction answered 13/12, 2016 at 1:23 Comment(1)
It's compatible with other browsers! developer.mozilla.org/en-US/docs/Web/API/Document/…Assimilation

© 2022 - 2024 — McMap. All rights reserved.