如何使用javascript检测IE10?

时间:2022-05-27 16:50:05

As many already posted in other questions (also in jQuery documentation), the old jQuery.browser.version is 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 this or 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) {...

6 个解决方案

#1


17  

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中存在的特定功能,而不是早期版本中的功能。

#2


15  

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/

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 isIE10 will still be false. And @_jscript_version will only start with 10 in IE 10.

条件编译不会在非IE中运行,因此isIE10仍然是false。并且@_jscript_version只会在IE 10中以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 eval or Function to find out in a similar fashion:

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

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

DEMO: 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.isTheBrowser and IE.actualVersion (which is translated from internal values of JScript versions).

并访问IE.isTheBrowser和IE.actualVersion(从JScript版本的内部值转换)等属性。

#3


3  

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

#4


0  

The IE10 User-Agent String says

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.

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

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

为了帮助说明,这里是一个正则表达式,它只捕获“MSIE”标记值的第一个数字:

// 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:

这里有一个捕获“MSIE”令牌的全部值:

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

#5


0  

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

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

#6


0  

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';
}    

#1


17  

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中存在的特定功能,而不是早期版本中的功能。

#2


15  

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/

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 isIE10 will still be false. And @_jscript_version will only start with 10 in IE 10.

条件编译不会在非IE中运行,因此isIE10仍然是false。并且@_jscript_version只会在IE 10中以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 eval or Function to find out in a similar fashion:

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

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

DEMO: 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.isTheBrowser and IE.actualVersion (which is translated from internal values of JScript versions).

并访问IE.isTheBrowser和IE.actualVersion(从JScript版本的内部值转换)等属性。

#3


3  

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

#4


0  

The IE10 User-Agent String says

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.

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

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

为了帮助说明,这里是一个正则表达式,它只捕获“MSIE”标记值的第一个数字:

// 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:

这里有一个捕获“MSIE”令牌的全部值:

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

#5


0  

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

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

#6


0  

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';
}