现象

请在 IE 下的非 iframe 页面中运行以下代码:

alert(window === window.top); // 1
alert(self === window.top); // 2
alert(self === window); // 3
alert(window == window.top); // 4
alert(typeof self === typeof window); // 5
alert(self == window); // 6

分析

上面第 5 和第 6 行的结果都为 true. 按照 ECMA-262 的定义,按理说 self === window 应该为 true.
然而测试结果告诉我们:在 IE 下,不是这样!

结论

IE 下,top, self, parent 和对应的 window 并不全等。涉及到这些值,用 === 比较时,要谨慎。
原因没想明白。就如 IE 下一些诡异的 CSS Hack 一样,要真弄明白,也许只能去找比尔盖茨要一份 IE 的源码了。

幸运的 jQuery 和倒霉的 YUI

jQuery 1.3.2 模拟 DOMReady 的代码中

// If IE and not an iframe
// continually check to see if the document is ready
if ( document.documentElement.doScroll && window == window.top ) (function(){
    if ( jQuery.isReady ) return;
    ...

John Resig 的代码里,布满了 === (这也是 Douglas Crockford 推荐的,能避免歧义性,同时有利于性能优化)。上面的 window == window.top 在满眼的 === 中间是有点突兀的。这个突兀,John Resig 并没有给出说明。估计很可能是遇见了 IE 下这个冷门 bug, 才减去了一个 = 号。

非常倒霉的是,YUI 为了解决 DOMReady 在 iframe 中的问题,最新开发中的 代码 中,不幸踩雷:

if (EU.isIE) {
    if (window !== window.top) {
        document.onreadystatechange = function() {
            if (document.readyState == 'complete') {
                document.onreadystatechange = null;
                EU._ready();
            }
        };
    } else {
        // doScroll method
    }
..

注:此 bug 已反馈给 YUI (ticket). 在即将发布的 2.8 中,应该能解决。