IE 错误(窗口 === 顶部) === 假 [英] IE Bug (window === top) === false
问题描述
在 IE 中.
window === top; // false
window === window.window // false
window == top; // true
window == window.window // true
在 FF3.6 和Chrome 稳定,这不会发生.
In FF3.6 & Chrome stable this doesn't happen.
在 IE 中 typeof
、.toString
、Object.prototype.toString.call
都返回相同的 top代码> &<代码>窗口代码>
In IE typeof
, .toString
, Object.prototype.toString.call
all return the same for both top
& window
这与这个有关.
谁能告诉我为什么 IE 不能做严格的对等?
Can anyone tell me why IE can't do strict equivelance?
请注意,循环引用不会在 IE 和 IE 中引起问题.铬.
Note that circular reference doesn't cause issues in both IE & Chrome.
o = {};
o.o = o;
o === o.o; // true
结果
window.window === window.top; // true
window.window === window.self; // true
所以它自己获取 window
是一个问题.
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
}
}
这现在变得很愚蠢:
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
[进一步编辑]
结果是 window.frames 持有一个指向 ie 调试器的指针.因此,打开调试器会更改 window
对象.我必须做更多的测试.
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
更不用说window.external
就是玩的不好
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"
推荐答案
这并不完全是一个错误:宿主对象可以为所欲为,而 window
对象是一个特别复杂的野兽,既是代表浏览器窗口的对象,又是全局对象的别名.我会把这个归结为一个奇怪的问题,并在比较 Window
对象时避免使用严格的 ===
运算符.
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.
请注意,这不是JavaScript 很奇怪"的耸人听闻.除了作为全局对象,window
还是一个宿主对象,HTML5 之前的版本可以合法地(至少根据规范)按照它喜欢的方式运行.旧版本的 IE 充分利用了这种自由,并表现出许多没有任何规范的古怪行为.试图在不访问源代码的情况下理解这一切是毫无意义的练习.
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.
这篇关于IE 错误(窗口 === 顶部) === 假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!