jQuery 如何使用javascript检测IE10?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16366907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:00:45  来源:igfitidea点击:

How do I detect IE10 using javascript?

javascriptjquerycross-browserinternet-explorer-10

提问by periback2

As many already posted in other questions (also in jQuery documentation), the old jQuery.browser.versionis deprecated and works only in jquery1.3.

正如其他问题(也在 jQuery 文档中)中发布的许多内容一样,旧版本jQuery.browser.version已弃用,仅适用于 jquery1.3。

Do you know another simple way to detect it, that I can include in my code before:

你知道另一种检测它的简单方法吗,我之前可以在我的代码中包含它:

function handleInfoDivPopupVisibility(dynamicEleId, staticEleId){
var parentContainer = $('headerSummaryContainer');
var dynamicEle = $(dynamicEleId);
var staticEle = $(staticEleId);

if(isIE() && parentContainer){
    if (jQuery.browser.version != 10) { // I need to find a way to detect if it's IE10 here.
        parentContainer.style.overflow = 'visible'; 
    }
}
dynamicEle ? dynamicEle.style.display = '' : '';
if(dynamicEle && staticEle)
    gui_positionBelow(dynamicEle, staticEle);
}

Before you say it's duplicated question of thisor this, I'd like to reinforce that I don't want to use css hacks. Is there a way to detect it just as simple as I could do before?

在你说这是这个这个的重复问题之前,我想强调一下我不想使用 css hacks。有没有办法像我以前一样简单地检测它?

if (jQuery.browser.version != 10) {...

回答by J0HN

In general it's a bad idea to check for browser version, it's considered a better practice to check for browser features. But if you're sure what you're doing:

一般来说,检查浏览器版本是一个坏主意,检查浏览器功能被认为是更好的做法。但是,如果您确定自己在做什么:

function getIEVersion(){
    var agent = navigator.userAgent;
    var reg = /MSIE\s?(\d+)(?:\.(\d+))?/i;
    var matches = agent.match(reg);
    if (matches != null) {
        return { major: matches[1], minor: matches[2] };
    }
    return { major: "-1", minor: "-1" };
}

var ie_version =  getIEVersion();
var is_ie10 = ie_version.major == 10;

We have the following code in production, so it works and well-tested.

我们在生产中有以下代码,因此它可以工作并经过良好测试。

And yes, we did have a need to detect IE10, not just a particular feature that exists in IE10 but not in earlier versions.

是的,我们确实需要检测 IE10,而不仅仅是 IE10 中存在但早期版本中不存在的特定功能。

回答by Ian

Internet Explorer has the feature of Conditional Compilation (http://www.javascriptkit.com/javatutors/conditionalcompile.shtml). You can use this:

Internet Explorer 具有条件编译功能 ( http://www.javascriptkit.com/javatutors/conditionalcompile.shtml)。你可以使用这个:

var isIE10 = false;
/*@cc_on
    if (/^10/.test(@_jscript_version)) {
        isIE10 = true;
    }
@*/
alert(isIE10);

DEMO:http://jsfiddle.net/X3Rvz/1/

演示:http : //jsfiddle.net/X3Rvz/1/

You can put this before all your JavaScript code, and from then on just reference isIE10.

你可以把它放在你所有的 JavaScript 代码之前,从那时起只引用isIE10.

The conditional compilation won't run in non-IE, so isIE10will still be false. And @_jscript_versionwill only start with 10in IE 10.

条件编译不会在非 IE 中运行,因此isIE10仍然会是false. 并且@_jscript_version只会10在 IE 10 中开始。

Conditional Comments aren't supported in IE 10, and the User-Agent string can be spoofed.

IE 10 不支持条件注释,并且可以欺骗用户代理字符串。

Since minification usually removes comments, you can use evalor Functionto find out in a similar fashion:

由于缩小通常会删除注释,因此您可以使用evalFunction以类似的方式查找:

var isIE10 = false;
if (Function('/*@cc_on return /^10/.test(@_jscript_version) @*/')()) {
    isIE10 = true;
}

DEMO:http://jsfiddle.net/wauGa/2/

演示:http : //jsfiddle.net/wauGa/2/



UPDATE:

更新:

To still avoid minification of comments but also combine detecting any version, you can use something like:

为了仍然避免评论的缩小,但同时结合检测任何版本,您可以使用以下内容:

var IE = (function () {
    "use strict";

    var ret, isTheBrowser,
        actualVersion,
        jscriptMap, jscriptVersion;

    isTheBrowser = false;
    jscriptMap = {
        "5.5": "5.5",
        "5.6": "6",
        "5.7": "7",
        "5.8": "8",
        "9": "9",
        "10": "10"
    };
    jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();

    if (jscriptVersion !== undefined) {
        isTheBrowser = true;
        actualVersion = jscriptMap[jscriptVersion];
    }

    ret = {
        isTheBrowser: isTheBrowser,
        actualVersion: actualVersion
    };

    return ret;
}());

And access the properties like IE.isTheBrowserand IE.actualVersion(which is translated from internal values of JScript versions).

并访问像IE.isTheBrowserIE.actualVersion这样的属性(从 JScript 版本的内部值转换而来)。

回答by Renier Cardoso

The jQuery.browser.version still works but you have to include the jquery-migrate plugin.

jQuery.browser.version 仍然有效,但您必须包含 jquery-migrate 插件。

http://api.jquery.com/jQuery.browser/https://github.com/jquery/jquery-migrate/#readme

http://api.jquery.com/jQuery.browser/ https://github.com/jquery/jquery-migrate/#readme

回答by Mike Samuel

The IE10 User-Agent Stringsays

IE10 用户代理字符串

However if your site is still using user-agent sniffing, then increasing the “MSIE” token to “10.0” is particularly noteworthy. Why? Because it adds an extra digit to the string value of the token. Most sites will handle this effortlessly, but some will process the extra digit incorrectly, causing them to identify IE10 as IE1.

To help illustrate, here's a regular expression that only captures the first digit of the “MSIE” token's value:

// INCORRECT: will report IE10 version in capture 1 as "1"
var matchIE = /MSIE\s(\d)/;

And here's one that captures the full value of the “MSIE” token:

// Correct: will report IE10 version in capture 1 as "10.0"
var matchIE = /MSIE\s([\d.]+)/

但是,如果您的站点仍在使用用户代理嗅探,那么将“MSIE”令牌增加到“10.0”尤其值得注意。为什么?因为它为令牌的字符串值添加了一个额外的数字。大多数站点会毫不费力地处理这个问题,但有些站点会错误地处理额外的数字,导致它们将 IE10 识别为 IE1。

为了帮助说明,这里有一个仅捕获“MSIE”令牌值的第一位数字的正则表达式:

// INCORRECT: will report IE10 version in capture 1 as "1"
var matchIE = /MSIE\s(\d)/;

这是捕获“MSIE”令牌全部价值的一个:

// Correct: will report IE10 version in capture 1 as "10.0"
var matchIE = /MSIE\s([\d.]+)/

回答by gulawaiz

Here is a one line solution to detect IE 10

这是检测 IE 10 的单行解决方案

var IE10 = navigator.userAgent.toString().toLowerCase().indexOf("trident/6")>-1;

and for more information on other version and browsers please refer this Link of Browser Detection

有关其他版本和浏览器的更多信息,请参阅此浏览器检测链接

回答by GM-Script-Writer-62850

I Found myself having a issue (border radius on frameset with legend) with IE 9 through 11 (did not check IE 12)
MSIE is no long user agent in IE 11 and the appName is Mozilla, however trident is in there I managed to extract the trident version # and detect it

我发现自己在使用 IE 9 到 11(没有检查 IE 12)时遇到问题(带有图例的框架集的边界半径)
MSIE 不再是 IE 11 中的用户代理,并且 appName 是 Mozilla,但是我设法提取了三叉戟三叉戟版本#并检测它

if(navigator.appVersion.indexOf('Trident/')>-1){// Begin stupid IE crap
    var IE=navigator.appVersion;
    IE=IE.slice(IE.indexOf('Trident/')+8);
    IE=IE.slice(0,IE.indexOf(';'));
    IE=Number(IE);
    if(IE>=5&&IE<=7) // IE 9 through IE 11
        document.body.className='ie';
}