<?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>Leeiio Chaos Made. &#187; IE判定</title>
	<atom:link href="http://leeiio.me/tag/ie%e5%88%a4%e5%ae%9a/feed/" rel="self" type="application/rss+xml" />
	<link>http://leeiio.me</link>
	<description>The world is golden.</description>
	<lastBuildDate>Thu, 26 Apr 2012 03:54:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>全世界最短的 JavaScript 判定 IE 浏览器！这个可以短！</title>
		<link>http://leeiio.me/ie_detection_in_5_bytes/</link>
		<comments>http://leeiio.me/ie_detection_in_5_bytes/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 17:50:17 +0000</pubDate>
		<dc:creator>Leeiio</dc:creator>
				<category><![CDATA[Skill | 技术]]></category>
		<category><![CDATA[IE判定]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://leeiio.me/?p=1818</guid>
		<description><![CDATA[有些东西你想让它长它并不一定就会如愿，但是有一些东西你想让它短也并不一定就会如愿，这两者都要看你的本事。对于我等IT民工来说，自然是代码写得越短越好。 你知道，IE是个脑残儿浏览器，不管是什么版本，纵使版本号越来越大，可总是让人觉得这玩意儿跟标准老是扯不上暧昧。可是，你知道，只要世上还有 windows 操作系统，IE就永生不灭，阿门。对于代码工作者来说，自然是苦不堪言，为了考虑IE的兼容问题，不管是写 CSS 还是 JS，往往都要对 IE 特别对待，这就少不了做些判断。本文不讨论如何区分 IE 的样式，仅是 JS 判定 IE 浏览器。 这个目前世界上最短的 Javascript 判定 IE 浏览器的方法来自一个俄人,又是俄人！它已经在各版本的 IE 以及目前其他流行的浏览器上经过测试，基于 IE 的 Bug，微软虽然已经意识到，但是从来没有纠正过。 &#60;script type='text/javascript'&#62; var ie = !-&#91;1,&#93;; alert&#40;ie&#41;; &#60;/script&#62; 以上代码运行结果：IE 下返回true，其他标准浏览器返回false。!-[1,]，仅仅只有 6 bytes！ 不过如果反过来判断，标准浏览器返回 true 而 &#8230; <a href="http://leeiio.me/ie_detection_in_5_bytes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://imgs.leeiio.me/blogimgs/2010/01/20100131_theShortestDefinitionOfIE.jpg" alt="全世界最短的JavaScript判定IE浏览器！" /><br />
有些东西你想让它长它并不一定就会如愿，但是有一些东西你想让它短也并不一定就会如愿，这两者都要看你的本事。对于我等IT民工来说，自然是代码写得越短越好。</p>
<p>你知道，IE是个脑残儿浏览器，不管是什么版本，纵使版本号越来越大，可总是让人觉得这玩意儿跟标准老是扯不上暧昧。可是，你知道，只要世上还有 windows 操作系统，IE就永生不灭，阿门。对于代码工作者来说，自然是苦不堪言，为了考虑IE的兼容问题，不管是写 CSS 还是 JS，往往都要对 IE 特别对待，这就少不了做些判断。本文不讨论如何区分 IE 的样式，仅是 JS 判定 IE 浏览器。<br />
<span id="more-1818"></span><br />
这个目前世界上最短的 Javascript 判定 IE 浏览器的方法来自一个<a rel="nofollow" href="http://studioad.ru/blog/ie_detection_in_5_bytes/2010-01-08-103">俄人</a>,又是俄人！它已经在各版本的 IE 以及目前其他流行的浏览器上经过测试，基于 IE 的 Bug，微软虽然已经意识到，但是从来没有纠正过。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> ie <span style="color: #339933;">=</span> <span style="color: #339933;">!-</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>ie<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>以上代码运行结果：IE 下返回true，其他标准浏览器返回false。!-[1,]，仅仅只有 6 bytes！</p>
<p>不过如果反过来判断，标准浏览器返回 true 而 IE 返回 false的话，则可以再缩短一个byte。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span><span style="color: #339933;">&gt;</span>
notIe <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// 标准浏览器代码</span>
<span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// IE Only的代码</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p><strong>看完了这些，你们是不是很好奇这些是怎么运行的？请继续看下文。</strong></p>
<p>这个 Bug 产生的原因是 IE 会添加一个空数组元素到数组元素的总数里。</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #009900;">&#93;</span>. <span style="color: #660066;">Length</span></pre></div></div>

<p>标准浏览器会返回 1 (基于标准的 ECMAscript ，在数组最后的逗号&#8221;,&#8221;会被忽略，这是为了方便在一列里显示以及自动生成等)，但是 IE 会返回 2。当你打印这个数组的时候 IE 将会返回 &#8220;1, &#8220;，也就是两个元素，而其他标准浏览器将会返回 &#8220;1&#8243;。</p>
<p>这很容易验证，比如在 IE 和 FF中运行以下代码：</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span><span style="color: #339933;">&gt;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//这是8个字符判定IE</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>[1,]实际上浏览器的操作是toString()转换成字符串的操作，-[1,]是将字符串强制转换为数字。而 IE 将会返回 NaN，但是遗憾的是 NaN 并不是一个数字，因为[1,]转换成字符串后的&#8221;1,&#8221;里面带有逗号。而其他标准浏览器会返回 -1，这是一个非 0 的数字。</p>
<p>你知道，NaN 转换成 Boolean 型将返回 false，所以-[1,]在 IE 下将返回 false。而任何非 0 的数字转换成 Boolean 型(例如-1)，在 标准浏览器下都将返回 true。所以我们得到了一个判定结果，!-[1,]在 IE 下返回true，而在其他标准浏览器下返回 false。也就达到了区分判定 IE 浏览器的目的。</p>
<p>当然，如前文所说，这个 Bug 其实微软很早就已经知道，但是却一直没有去修复它，所以在未来的 > IE8 的 IE 浏览器也就不确定是否依旧可以，不过基本上这么多代的 IE 都没有修复，未来的 IE 也不太会去修复的样子。</p>
<p><strong>以下是其他的一些区分判定 IE 浏览器的代码，也可以参考下:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">'text/javascript'</span><span style="color: #339933;">&gt;</span>
<span style="color: #006600; font-style: italic;">// Option from Dean Edwards:</span>
<span style="color: #003366; font-weight: bold;">var</span> ie <span style="color: #339933;">=</span> <span style="color: #006600; font-style: italic;">/*@cc_on!@*/</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Use the commented line:</span>
<span style="color: #003366; font-weight: bold;">var</span> ie<span style="color: #006600; font-style: italic;">//@cc_on=1;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Variation (shorter variable):</span>
<span style="color: #003366; font-weight: bold;">var</span> ie <span style="color: #339933;">=</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\v</span>'</span><span style="color: #339933;">==</span><span style="color: #3366CC;">'v'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/ /</span> Option to Gareth Hayes <span style="color: #009900;">&#40;</span>former record<span style="color: #339933;">-</span>holder<span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
<span style="color: #003366; font-weight: bold;">var</span> ie <span style="color: #339933;">=</span> <span style="color: #339933;">!+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\v</span>1&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span> script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>参考文章：<br />
<a href="http://studioad.ru/blog/ie_detection_in_5_bytes/2010-01-08-103" rel="nofollow">Самое короткое определение IE</a><br />
<a href="http://webreflection.blogspot.com/2009/01/32-bytes-to-know-if-your-browser-is-ie.html" rel="nofollow">32 bytes, ehr &#8230; 9, ehr &#8230; 7!!! to know if your browser is IE</a></p>
]]></content:encoded>
			<wfw:commentRss>http://leeiio.me/ie_detection_in_5_bytes/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
	</channel>
</rss>

