There's a convoluted backstory involving how I came across this, but why is the
self property not exactly equal to the window itself?
In Safari and Firefox and friends, the results are as I'd expect:
> window == window.self true > window === window.self true
The same isn't true in Internet Explorer, though:
>> window == window.self true >> window === window.self false
Can anybody account for the inconsistency? To exactly what is the
self property of the window object pointing? It casts to something with equality, which is even more vexing.
That's not all,
I believe what we're probably seeing here is the difference between the ‘inner window’ and ‘outer window’ objects. Certainly other browsers have these (eg. Moz); they're typically used to present a different view of the
window from inside and outside its own code.
The inner window holds your global variables and document-specific members. The outer window is accessible to [cross-frame-] scripting via window-references like
opener, and apparently
self. It is bound to the owner viewport (browser window/frame), so eg. when you navigate an iframe to a new document, the parent document still sees the same-identity
window object in its iframe.
form with an element called
So for compatibility, the split window has to continue to appear to be a single object to scripts even though it isn't underneath. In IE, sometimes the mask slips: it seems like saying
window gets you the inner window, and there's no hack to make it
=== against the outer window.
ETA: Actually come to think of it, there's even some (poor) justification for this. The ECMAScript spec, which is not written with multiple global contexts in mind, defines
window, and the unbound version of
this, as retrieving the global variable scope object, which would be the inner window.
The other properties, being part of the DOM/BOM, are not within the scope of the ECMA spec, so they can return something different (and must, for the sake of cross-frame scripting).
Nice question. It does it with
window == document; // true window === document; // false window == document.parentWindow; // true window === document.parentWindow; // false
The big wtf for me though is:
window == document; // true document == window; // false
You can swap out
this in any of the above examples (and stay in the global context) with the same results. It makes the
== operator even more suspect.
Why it behaves this way is so beyond me.
©2020 All rights reserved.