James Padolsey在GitHub上放了一些代码片段,我将在这里引用:
// ----------------------------------------------------------// A short snippet for detecting versions of IE in Javascript// without resorting to user-agent sniffing// ----------------------------------------------------------// If you're not in IE (or IE version is less than 5) then:// ie === undefined// If you're in IE (>=5) then you can determine which version:// ie === 7; // IE7// Thus, to detect IE:// if (ie) {}// And to detect the version:// ie === 6 // IE6// ie > 7 // IE8, IE9 ...// ie < 9 // Anything less than IE9// ----------------------------------------------------------// UPDATe: Now using Live NodeList idea from @jdaltonvar ie = (function(){ var undef, v = 3, div = document.createElement('div'), all = div.getElementsByTagName('i'); while ( div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', all[0] ); return v > 4 ? v : undef;}());当然,所有的荣誉都应该归詹姆斯所有,我只是信使(但如果我的复制粘贴操作有误,请射击信使)。
还要看看创建的叉子。保罗·爱尔兰(PaulIrish)在评论中解释了内部工作原理。


![Javascript IE检测,为什么不使用简单的条件注释?[重复] Javascript IE检测,为什么不使用简单的条件注释?[重复]](http://www.mshxw.com/aiimages/31/464817.png)
