47

为了检测 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>

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

if(window.isIE)
   ...

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


仅供参考:我知道最好使用对象检测而不是浏览器检测,但是在某些情况下您仍然必须使用浏览器检测。

4

15 回答 15

58

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;

}());

当然,所有的功劳都应该归詹姆斯所有,我只是信使(但如果我的复制粘贴操作出错,请射击信使)。

还要查看创建的分叉。Paul Irish 在评论中解释了内部工作原理。

于 2010-11-19T18:33:11.817 回答
39

如果您想这样做,我认为使用条件编译会更好,因为您可以在 javascript 中执行此操作而无需更改 html:

var isIE = /*@cc_on!@*/false;
于 2010-11-12T21:40:15.193 回答
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);
}());

更新为与 IE11 一起使用。感谢“acarlon”指出它不起作用,感谢“mario”指出我修复的代码!

于 2013-05-20T20:37:24.243 回答
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;
}
于 2014-01-02T10:52:02.890 回答
6

我想我有你要找的东西。您可以使用 Javascript 和 clientCaps 以字符串“AA.BB.CCCC.DDDD”的形式获取 Internet Explorer 的完整版本。

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

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

我目前不确定 IE Mobile 的行为方式,但您始终可以使用备份检测方法,以防此 clientCaps 方法失败。

到目前为止,我得说,它运作良好。

于 2012-10-21T13:59:48.843 回答
4

我想你回答了你自己的问题:首先,它只检测 IE,所以脚本本质上会将浏览器的世界分成两部分:IE 和 <everythingelse>。

其次,您必须为每个 HTML 页面添加一个看起来很古怪的评论。鉴于 jQuery 和 YUI 等范围广泛的 JavaScript 库必须“易于”插入/用于广泛的站点,因此您会自然而然地使它们更难使用。

于 2010-11-12T21:45:24.480 回答
3

navigator.userAgent如果确实需要浏览器检测(而不是特征检测),则存在,并且 jQuery 使用它来获取$.browser对象的信息。这比在每个页面中都包含特定于 IE 的条件注释要好得多。

于 2010-11-12T21:34:57.407 回答
2

在这里您可以找到一些非常简单的浏览器检测技巧:http ://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/

var isIE = IE='\v'=='v';
于 2013-06-18T23:08:15.197 回答
2
var version = navigator.userAgent.match(/(msie) (\d+)/i);
console.log(version);

我看了这个问题后很快就写了一些东西,以防有人想要它。

** 编辑 **

根据Johnny Darvall 在下面的评论,我正在为任何试图嗅探Internet Explorer 11的人添加一个链接:

http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx

于 2013-07-11T22:02:52.767 回答
2

我正在使用该代码

var isIE = navigator.userAgent.indexOf('MSIE') > -1;

于 2013-07-16T14:46:05.630 回答
1

检查浏览器是个坏主意 - 最好改为检查浏览器功能。例如,通常您检查用户是否正在使用 IE,因为您想使用 IE 不支持的某些功能。但是,您知道所有当前和未来的非 IE 浏览器都将支持该功能吗?不,因此 jQuery 使用的方式更好:它创建并执行小型测试用例检查某些错误/功能 - 您可以简单地检查 if(browser_supports_XYZ) 之类的东西,而不是检查用户是否使用特定的浏览器。

无论如何,总有需要检查浏览器的情况,因为这是一个视觉错误,您无法使用脚本进行测试。在这种情况下,最好使用 javascript 而不是条件注释,因为您可以让浏览器在您需要它的位置而不是在其他地方进行检查(想象一个 .js 文件,您在其中检查 isIE 从未在该文件中定义)

于 2010-11-12T21:41:13.983 回答
0

对于我的用例,我真的只需要检测是否低于 IE9,所以我使用

if (document.body.style.backgroundSize === undefined && navigator.userAgent.indexOf('MSIE') > -1)
{
//IE8- stuff
}
于 2014-03-08T00:14:08.703 回答
-1
  • 它污染了全局命名空间
  • 它需要更改两个文件。
  • 它仅适用于 IE
  • 从技术上讲,条件注释仍然是注释
于 2010-11-12T21:48:03.210 回答
-1

这个效果很好,
var isIe = !!window.ActiveXObject;

于 2011-09-24T23:40:45.307 回答
-1

你为什么不直接用 HTML5 编程,然后检查一下

if ( window.navigator.product !== "Gecko" )

?? 诚然,这将包括“Gecko”系列中的 IE11,但它现在不应该足够好吗?

注意:HTML5 规范。说 navigator.product 必须返回“Gecko”......并且 IE10 及更早版本都返回其他内容。

于 2015-01-03T16:14:58.467 回答