当我想检测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”。我如何检测它?


当前回答

我用了一个更简单的方法:

导航器全局对象有一个属性touchpoints,在ie11中称为msMaxTouchPoints tho。

所以如果你寻找:

navigator.msMaxTouchPoints !== void 0 

你会找到Internet Explorer 11。

其他回答

这似乎是一种更好的方法。如果没有匹配,"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';
}

我在带有滚动条的元素上使用了onscroll事件。当在IE中触发时,我添加了以下验证:

onscroll="if (document.activeElement==this) ignoreHideOptions()"

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

感谢上帝IE消失了…

仅适用于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;
var ua = navigator.userAgent.toString().toLowerCase();
var match = /(trident)(?:.*rv:([\w.]+))?/.exec(ua) ||/(msie) ([\w.]+)/.exec(ua)||['',null,-1];
var rv = match[2];
return rv;