Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

javascript - IE Bug (window === top) === false

In IE.

window === top; // false
window === window.window // false
window == top; // true
window == window.window // true

In FF3.6 & Chrome stable this doesn't happen.

In IE typeof, .toString, Object.prototype.toString.call all return the same for both top & window

This is related to this.

Can anyone tell me why IE can't do strict equivelance?

Note that circular reference doesn't cause issues in both IE & Chrome.

o = {};
o.o = o;
o === o.o; // true

Turns out

window.window === window.top; // true
window.window === window.self; // true

So it's an issue with getting window on it's own.

for (var i in window) {
    if (window.window[i] !== window[i]) {
        console.log(i); // external, frames, clipboardData
    }
}

[Edit]

This is just getting stupid now:

 window.frames === window.frames; // false
 window.frames == window.frames; // false
 window.external == window.external; // true
 window.external === window.external; // false
 window.clipboardData === window.clipboardData; // false
 window.clipboardData == window.clipboardData; // false

[Further edit]

turns out that window.frames holds a pointer to the ie debugger. So having the debugger open changes the window object. I have to do some more testing.

window.frames.location === window.frames.location; // false
window.frames.location == window.frames.location; // true
window.frames.event.boundElements == window.frames.event.boundElements; // false

Not to mention that window.external just does not play nicely

>>for (var i in window.external) alert(i);
"Object doesn't support this action"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This isn't exactly a bug: host objects can do whatever they like, and the window object is a particularly complicated beast, serving the dual purposes of being the object that represents the browser window and also being an alias for the global object. I'd chalk this one up as a weirdness and avoid using the strict === operator when comparing Window objects.

Note that this isn't a "JavaScript is weird" shrugpost. As well as serving as the global object, window is a host object and pre-HTML5 could legitimately (according to spec, at least) behave however it liked. Older versions of IE take advantage of this freedom and exhibit much quirky behaviour for which there is no specification whatsoever. Trying to understand it all without access to the source code is a pointless exercise.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...