Javascript IE检测,为什么不使用简单的条件注释?

Mar*_*aio 47 javascript internet-explorer browser-detection conditional-comments

为了检测IE,大多数Javascript库都做了各种各样的技巧.

  • jQuery似乎在页面的DOM中添加了一个临时对象来检测某些功能,
  • YUI2对其YAHOO.env.ua = function()(文件yahoo.js)中的用户代理进行正则表达式

在阅读了这个答案之后,我想到了这是真的,为了在Javascript中简单地检测IE,我们可以简单地添加到我们的页面:

<!--[if IE]><script type="text/javascript">window['isIE'] = true;</script><![endif]-->

<script type="text/javascript" src="all-your-other-scripts-here.js"></script>
Run Code Online (Sandbox Code Playgroud)

现在,window.isIE为我们所有的Javascript代码设置变量,只需执行以下操作:

if(window.isIE)
   ...
Run Code Online (Sandbox Code Playgroud)

除了这可能导致痛苦,因为它必须在所有页面中添加之外,是否存在我可能不知道的任何问题/考虑因素?


仅供参考:我知道最好使用对象检测而不是浏览器检测,但有些情况下您仍然需要使用浏览器检测.

Mar*_*pel 59

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 @jdalton

var 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;

}());
Run Code Online (Sandbox Code Playgroud)

当然所有的积分应该归詹姆斯,我只是信使(但如果我的复制粘贴动作错误,请拍摄信使).

还要看一下创建的叉子.Paul Irish在评论中解释了内部运作.

  • 条件语句[IE10不再支持](http://blogs.msdn.com/b/ie/archive/2011/07/06/html5-parsing-in-ie10.aspx). (11认同)

Alf*_*oML 40

如果你想这样做,我认为使用条件编译要好得多,因为你可以在javascript内部进行,而无需更改html:

var isIE = /*@cc_on!@*/false;
Run Code Online (Sandbox Code Playgroud)

  • 请记住,缩小器不喜欢这种想法; YUI Compressor生气,Google Closure Compiler对它嗤之以鼻. (19认同)
  • @AlfonsoML那是什么奇怪的黑客?:) (7认同)
  • @AlfonsoML多年来我一直在阅读条件注释和userAgent字符串,现在你告诉我你可以用这一行检测IE吗?:p (5认同)
  • 我在Windows 7和8上的IE10上尝试过它并没有用. (4认同)
  • 这有效.但是,与条件注释不同,使用条件编译时,不可能区分IE的版本,只能区分不同版本的JScript引擎(可以独立于IE版本进行交换和替换). (2认同)

Sea*_*mbo 26

Marcel Korpel的答案不再适用(在IE 10中它返回undef,因此IE 10看起来不是IE).注意:现在更新以使用IE 11.

这是该代码的变体,但它来自Microsoft的建议.如果您使用的是以前的代码,则可以直接使用此替换代码,因为它的构建方式与完全相同.

与条件注释/编译不同,它也应该与最小化器一起使用.

// ----------------------------------------------------------
// 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, IE10 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------
var ie = (function(){
    var undef,rv = -1; // Return value assumes failure.
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf('MSIE ');
    var trident = ua.indexOf('Trident/');

    if (msie > 0) {
        // IE 10 or older => return version number
        rv = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
    } else if (trident > 0) {
        // IE 11 (or newer) => return version number
        var rvNum = ua.indexOf('rv:');
        rv = parseInt(ua.substring(rvNum + 3, ua.indexOf('.', rvNum)), 10);
    }

    return ((rv > -1) ? rv : undef);
}());
Run Code Online (Sandbox Code Playgroud)

更新以使用IE11.感谢'acarlon'指出它无法正常工作,并且'mario'代码我基于修复程序!

  • 但这适用于User-Agent字符串嗅探; UA字符串很容易被欺骗,但条件注释不能.无论如何,谁需要检测IE 10?您现在可以使用功能检测来测试所有内容,这远远超过浏览器检测(仅用于检测通过功能检测无法检测到的内容). (2认同)
  • @Marcel如果你想假装成别人,请随意.这应该是帮助普通的IE白痴(谁不会做欺骗).如果你想假装,那除了(可能)你自己不会伤害任何人. (2认同)
  • 这不再适用于IE11,因为navigator.appName不是M $.这个答案有效:http://stackoverflow.com/a/21712356/746754 (2认同)

web*_*r55 11

IE 11已经发生了很大的变化,现在许多过去的浏览器检测方法都不起作用.以下代码适用于IE 11及更早版本.

function isIE()
{
    var isIE11 = navigator.userAgent.indexOf(".NET CLR") > -1;      
    var isIE11orLess = isIE11 || navigator.appVersion.indexOf("MSIE") != -1;
    return isIE11orLess;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我想我有你想要的东西.您可以使用Javascript和clientCaps将完整版本的Internet Explorer作为字符串"AA.BB.CCCC.DDDD".

http://www.pinlady.net/PluginDetect/IE/

它似乎适用于IE 5.5及更高版本(包括IE 10).它不受navigator.userAgent /文档模式/浏览器模式的影响.不需要条件注释或任何额外的HTML元素.这是一个纯粹的Javascript解决方案.

我现在不确定IE Mobile的行为方式,但是如果这个clientCaps方法失败,你总是可以使用备份检测方法.

到目前为止,我得说,它运作得很好.