如何使用javascript检测IE10?

per*_*ck2 12 javascript jquery cross-browser internet-explorer-10

正如许多已经在其他问题(也在jQuery文档中)中发布的那样,旧jQuery.browser.version的已被弃用,仅适用于jquery1.3.

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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

if (jQuery.browser.version != 10) {...
Run Code Online (Sandbox Code Playgroud)

J0H*_*0HN 17

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

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;
Run Code Online (Sandbox Code Playgroud)

我们在生产中有以下代码,因此它可以正常运行并经过充分测试.

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

  • 好吧,我们有相同的情况:) 10年前写的应用程序,旨在只支持IE,作为当前项目的一部分,我们应该添加跨浏览器支持,现代样式,MVC等等.所以我完全了解你的情况:) (3认同)

Ian*_*Ian 15

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

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

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

您可以在所有JavaScript代码之前添加此代码,然后再引用它isIE10.

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

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

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

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

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


更新:

为了避免缩小注释,同时结合检测任何版本,您可以使用以下内容:

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;
}());
Run Code Online (Sandbox Code Playgroud)

和访问属性等IE.isTheBrowserIE.actualVersion(从JScript的版本的内部值翻译).

  • [这个答案](http://stackoverflow.com/a/15657729/778118)建议你一个解决方法,如果你正在缩小你的脚本... (3认同)
  • @Pointy [自IE10起不再存在HTML条件评论](http://msdn.microsoft.com/en-us/library/ie/hh801214(v = vs.85).aspx). (2认同)