当我想检测IE时,我使用以下代码:

function getInternetExplorerVersion()
{
  var rv = -1;
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    msg = "You are using IE " + ver;
  }
  alert( msg );
}

但是IE11返回“您没有使用Internet Explorer”。我如何检测它?


当前回答

使用MSInputMethodContext作为特性检测检查的一部分。例如:

//Appends true for IE11, false otherwise
window.location.hash = !!window.MSInputMethodContext && !!document.documentMode;

参考文献

MSInputMethodContext对象 输入法编辑器API 我希望在IE12中看到什么:Internet Explorer 12的错误修复和新功能的愿望清单 打印稿/ lib.dom.d。·微软/TypeScript IE DOM api

其他回答

仅适用于IE浏览器:

var ie = 'NotIE'; //IE5-11, Edge+
    if( !!document.compatMode ) {
        if( !("ActiveXObject" in window) ) ) ie = 'EDGE';
        if( !!document.uniqueID){
            if('ActiveXObject' in window && !window.createPopup ){ ie = 11; }
            else if(!!document.all){
                    if(!!window.atob){ie = 10;}
                    else if(!!document.addEventListener) {ie = 9;}
                    else if(!!document.querySelector){ie = 8;}
                    else if(!!window.XMLHttpRequest){ie = 7;}
                    else if(!!document.compatMode){ie = 6;}
                    else ie = 5;
                }
        }
    }

使用alert (ie);

测试:

var browserVersionExplorer = (function() { var ie = '<s>NotIE</s>', me = '<s>NotIE</s>'; if (/msie\s|trident\/|edge\//i.test(window.navigator.userAgent) && !!(document.documentMode || document.uniqueID || window.ActiveXObject || window.MSInputMethodContext)) { if (!!window.MSInputMethodContext) { ie = !("ActiveXObject" in window) ? 'EDGE' : 11; } else if (!!document.uniqueID) { if (!!(window.ActiveXObject && document.all)) { if (document.compatMode == "CSS1Compat" && !!window.DOMParser ) { ie = !!window.XMLHttpRequest ? 7 : 6; } else { ie = !!(window.createPopup && document.getElementById) ? parseFloat('5.5') : 5; } if (!!document.documentMode && !!document.querySelector ) { ie = !!(window.atob && window.matchMedia) ? 10 : ( !!document.addEventListener ? 9 : 8); } } else ie = !!document.all ? 4 : (!!window.navigator ? 3 : 2); } } return ie > 1 ? 'IE ' + ie : ie; })(); alert(browserVersionExplorer);

2017年6月1日更新

现在我们可以用一些更简单的东西:

var uA = window.navigator.userAgent,
    onlyIEorEdge = /msie\s|trident\/|edge\//i.test(uA) && !!( document.uniqueID || window.MSInputMethodContext),
    checkVersion = (onlyIEorEdge && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;

使用MSInputMethodContext作为特性检测检查的一部分。例如:

//Appends true for IE11, false otherwise
window.location.hash = !!window.MSInputMethodContext && !!document.documentMode;

参考文献

MSInputMethodContext对象 输入法编辑器API 我希望在IE12中看到什么:Internet Explorer 12的错误修复和新功能的愿望清单 打印稿/ lib.dom.d。·微软/TypeScript IE DOM api

坦率地说,我会说使用你需要的库(比如platform.js)。在某些时候,事情会发生变化,库将为这些变化做好准备,使用正则表达式的手动解析将失败。

感谢上帝IE消失了…

这似乎是一种更好的方法。如果没有匹配,"indexOf"返回-1。它不会覆盖主体上现有的类,只是添加它们。

// add a class on the body ie IE 10/11
var uA = navigator.userAgent;
if(uA.indexOf('Trident') != -1 && uA.indexOf('rv:11') != -1){
    document.body.className = document.body.className+' ie11';
}
if(uA.indexOf('Trident') != -1 && uA.indexOf('MSIE 10.0') != -1){
    document.body.className = document.body.className+' ie10';
}

IE11不再报告为MSIE,根据这个更改列表,它是故意避免错误检测。

如果你真的想知道它是IE,你可以做的是在用户代理if导航器中检测Trident/字符串。appName返回Netscape,类似于(未测试的);

function getInternetExplorerVersion() { var rv = -1; if (navigator.appName == 'Microsoft Internet Explorer') { var ua = navigator.userAgent; var re = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})"); if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 ); } else if (navigator.appName == 'Netscape') { var ua = navigator.userAgent; var re = new RegExp("Trident/.*rv:([0-9]{1,}[\\.0-9]{0,})"); if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 ); } return rv; } console.log('IE version:', getInternetExplorerVersion());

请注意,IE11 (afaik)仍处于预览阶段,用户代理在发布前可能会发生变化。