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


当前回答

用这个检测大多数浏览器:

var getBrowser = function(){
  var navigatorObj = navigator.appName,
      userAgentObj = navigator.userAgent,
      matchVersion;
  var match = userAgentObj.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
  if( match && (matchVersion = userAgentObj.match(/version\/([\.\d]+)/i)) !== null) match[2] = matchVersion[1];
  //mobile
  if (navigator.userAgent.match(/iPhone|Android|webOS|iPad/i)) {
    return match ? [match[1], match[2], mobile] : [navigatorObj, navigator.appVersion, mobile];
  }
  // web browser
  return match ? [match[1], match[2]] : [navigatorObj, navigator.appVersion, '-?'];
};

https://gist.github.com/earlonrails/5266945

其他回答

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

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

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

所以如果你寻找:

navigator.msMaxTouchPoints !== void 0 

你会找到Internet Explorer 11。

解决方案:

function GetIEVersion() { var sAgent = window.navigator.userAgent; var Idx = sAgent.indexOf("MSIE"); // If IE, return version number. if (Idx > 0) return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx))); // If IE 11 then look for Updated user agent string. else if (!!navigator.userAgent.match(/Trident\/7\./)) return 11; else return 0; //It is not IE } if ((GetIEVersion() > 0) || (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)){ alert("This is IE " + GetIEVersion()); }else { alert("This no is 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;

在window中使用!(window.ActiveXObject) && "ActiveXObject"显式检测IE11。

要检测任何IE (pre-Edge,“Trident”)版本,在窗口中使用“ActiveXObject”代替。