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


当前回答

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

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

其他回答

仅适用于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;

解决方案:

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 "); }

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

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

所以如果你寻找:

navigator.msMaxTouchPoints !== void 0 

你会找到Internet Explorer 11。

使用DetectOS.js。这是一个简单的JS定义,适用于没有依赖关系的流行操作系统和浏览器:

class DetectOS { constructor() { this.browser = this.searchString(this.dataBrowser()) this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) this.OS = this.searchString(this.dataOS()) } searchString(data) { for (let i = 0; i < data.length; i++) { let dataString = data[i].string, dataProp = data[i].prop this.versionSearchString = data[i].versionSearch || data[i].identity if (dataString) { if (dataString.indexOf(data[i].subString) !== -1) { return data[i].identity } } else if (dataProp) { return data[i].identity } } } searchVersion(dataString) { let index = dataString.indexOf(this.versionSearchString) if (index === -1) return return parseFloat(dataString.substring(index+this.versionSearchString.length + 1)) } dataBrowser() { return [ /*************** * Chrome ***************/ { string: navigator.userAgent, subString: "Chrome", identity: "Chrome" }, /*************** * Safari ***************/ { string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" }, /*************** * For Older Opera (12.18-) ***************/ { prop: window.opera, identity: "Opera", versionSearch: "Version" }, /*************** * Internet Explorer 10 ***************/ { string: navigator.userAgent, subString: "MSIE", identity: "IE10", versionSearch: "MSIE" }, /*************** * Internet Explorer 11 ***************/ { string: navigator.userAgent, subString: "Trident", identity: "IE11", versionSearch: "rv" }, /*************** * Edge ***************/ { string: navigator.userAgent, subString: "Edge", identity: "Edge", versionSearch: "Edge" }, /*************** * Firefox ***************/ { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" }, /*************** * For Older Netscapes (4-) ***************/ { string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" }, /*************** * For Newer Netscapes (6+) ***************/ { string: navigator.userAgent, subString: "Netscape", identity: "Netscape" }, /*************** * Other Browsers ***************/ { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" }, { string: navigator.vendor, subString: "iCab", identity: "iCab" }, { string: navigator.vendor, subString: "KDE", identity: "Konqueror" }, { string: navigator.vendor, subString: "Camino", identity: "Camino" } ] } dataOS() { return [ { string: navigator.platform, subString: 'Win', identity: 'Windows' }, { string: navigator.platform, subString: 'Mac', identity: 'macOS' }, { string: navigator.userAgent, subString: 'iPhone', identity: 'iOS' }, { string: navigator.userAgent, subString: 'iPad', identity: 'iOS' }, { string: navigator.userAgent, subString: 'iPod', identity: 'iOS' }, { string: navigator.userAgent, subString: 'Android', identity: 'Android' }, { string: navigator.platform, subString: 'Linux', identity: 'Linux' } ] } } const Detect = new DetectOS() console.log("We know your browser – it's " + Detect.browser + " " + Detect.version); console.log("We know your OS – it's " + Detect.OS); console.log("We know everything about you.");