<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>岁月如歌 &#187; self</title>
	<atom:link href="http://lifesinger.org/blog/tag/self/feed/" rel="self" type="application/rss+xml" />
	<link>http://lifesinger.org/blog</link>
	<description>关注用户体验、前端开发，记录生活点滴、岁月足迹。</description>
	<lastBuildDate>Mon, 06 Sep 2010 15:05:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>IE 下，window 和 self 的不全等</title>
		<link>http://lifesinger.org/blog/2009/08/window-self-in-ie/</link>
		<comments>http://lifesinger.org/blog/2009/08/window-self-in-ie/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 14:02:42 +0000</pubDate>
		<dc:creator>lifesinger</dc:creator>
				<category><![CDATA[开发]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[self]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://lifesinger.org/blog/?p=2108</guid>
		<description><![CDATA[现象 请在 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 [...]]]></description>
			<content:encoded><![CDATA[<h3>现象</h3>
<p>请在 IE 下的非 iframe 页面中运行以下代码：</p>
<pre>
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
</pre>
<h3>分析</h3>
<p>上面第 5 和第 6 行的结果都为 true. 按照 ECMA-262 的定义，按理说 self === window 应该为 true.<br />
然而测试结果告诉我们：在 IE 下，不是这样！</p>
<h3>结论</h3>
<p>IE 下，top, self, parent 和对应的 window 并不全等。涉及到这些值，用 === 比较时，要谨慎。<br />
原因没想明白。就如 IE 下一些诡异的 CSS Hack 一样，要真弄明白，也许只能去找比尔盖茨要一份 IE 的源码了。</p>
<h3>幸运的 jQuery 和倒霉的 YUI</h3>
<p>jQuery 1.3.2 模拟 DOMReady 的代码中<span id="more-2108"></span>，</p>
<pre>
// If IE and not an iframe
// continually check to see if the document is ready
if ( document.documentElement.doScroll &#038;&#038; window == window.top ) (function(){
    if ( jQuery.isReady ) return;
    ...
</pre>
<p>John Resig 的代码里，布满了 === （这也是 Douglas Crockford 推荐的，能避免歧义性，同时有利于性能优化）。上面的 window == window.top 在满眼的 === 中间是有点突兀的。这个突兀，John Resig 并没有给出说明。估计很可能是遇见了 IE 下这个冷门 bug, 才减去了一个 = 号。</p>
<p>非常倒霉的是，YUI 为了解决 <a href="http://lifesinger.org/blog/2009/08/yui-ondomready-iframe-bug/">DOMReady 在 iframe 中的问题</a>，最新开发中的 <a href="http://github.com/yui/yui2/blob/99b33610b410be5c17e1924b0f63b45b506633ae/src/event/js/Event.js">代码</a> 中，不幸踩雷：</p>
<pre>
if (EU.isIE) {
    if (window !== window.top) {
        document.onreadystatechange = function() {
            if (document.readyState == 'complete') {
                document.onreadystatechange = null;
                EU._ready();
            }
        };
    } else {
        // doScroll method
    }
..
</pre>
<p>注：此 bug 已反馈给 YUI (<a href="http://yuilibrary.com/projects/yui2/ticket/2528358">ticket</a>). 在即将发布的 2.8 中，应该能解决。</p>
]]></content:encoded>
			<wfw:commentRss>http://lifesinger.org/blog/2009/08/window-self-in-ie/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
