如何使用JavaScript检测Internet Explorer(IE)和Microsoft Edge?

71 html javascript internet-explorer media-queries microsoft-edge

我经常环顾四周,我知道有很多方法可以检测到Internet Explorer.

我的问题是这样的:我的HTML文档上有一个区域,点击后会调用与任何类型的Internet Explorer不兼容的JavaScript函数.我想检测是否正在使用IE,如果是,请将变量设置为true.

问题是,我用Notepad ++编写代码,当我在浏览器中运行HTML代码时,检测IE的方法都没有.我认为问题在于我是用Notepad ++运行的.我需要能够检测IE,因此基于该变量,我可以禁用该站点的该区域.我试过这个:

var isIE10 = false;

if (navigator.userAgent.indexOf("MSIE 10") > -1) {
    // this is internet explorer 10
    isIE10 = true;
   window.alert(isIE10);
}

var isIE = (navigator.userAgent.indexOf("MSIE") != -1);

if(isIE){
    if(!isIE10){
    window.location = 'pages/core/ie.htm';
    }
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.如何从Notepad ++中检测IE?这就是我正在测试HTML的方法,但我需要一种能够解决这个问题的方法.

编辑

我注意到有人将此标记为副本,这是可以理解的.我想我不清楚.我不能使用JQuery答案,所以这不是重复,因为我要求一个vanilla JS答案.

编辑#2

还有办法检测Microsoft Edge浏览器吗?

Sce*_*tic 87

这是我知道如何检查IE和Edge的最新正确方法:

if (/MSIE 10/i.test(navigator.userAgent)) {
   // This is internet explorer 10
   window.alert('isIE10');
}

if (/MSIE 9/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent)) {
    // This is internet explorer 9 or 11
    window.location = 'pages/core/ie.htm';
}

if (/Edge\/\d./i.test(navigator.userAgent)){
   // This is Microsoft Edge
   window.alert('Microsoft Edge');
}
Run Code Online (Sandbox Code Playgroud)

请注意,您的代码中不需要额外的var isIE10,因为它现在进行了非常具体的检查.

另请查看此页面以获取最新的IE和Edge用户代理字符串,因为此答案可能在某些时候过时:https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29. ASPX

  • FWIW,当我在Win10/Edge VM中检查`navigator.userAgent`时,我只得到它的第一部分,它不包含``Edge'字符串. (2认同)

skr*_*z14 29

我不知道为什么,但我没有像其他人所说的那样在userAgent中看到"Edge",所以我不得不采取另一种可能对某些人有帮助的途径.

我没有看navigator.userAgent,而是查看了navigator.appName来区分它是IE <= 10还是IE11和Edge.IE11和Edge使用"Netscape"的appName,而每隔一次迭代使用"Microsoft Internet Explorer".

在我们确定浏览器是IE11或Edge之后,我查看了navigator.appVersion.我注意到在IE11中,字符串很长,里面有很多信息.我随意挑出了"Trident"这个词,这个词肯定不在navigator.appVersion for Edge中.对这个词的测试让我能够区分这两个词.

下面是一个函数,它将返回用户所在的Internet Explorer的数值.如果在Microsoft Edge上,则返回数字12.

祝你好运,我希望这有帮助!

function Check_Version(){
    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer'){

       var ua = navigator.userAgent,
           re  = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");

       if (re.exec(ua) !== null){
         rv = parseFloat( RegExp.$1 );
       }
    }
    else if(navigator.appName == "Netscape"){                       
       /// in IE 11 the navigator.appVersion says 'trident'
       /// in Edge the navigator.appVersion does not say trident
       if(navigator.appVersion.indexOf('Trident') === -1) rv = 12;
       else rv = 11;
    }       

    return rv;          
}
Run Code Online (Sandbox Code Playgroud)

  • 这在edge和firefox中都返回12. (7认同)
  • 这不起作用.在Chrome,Opera,Firefox和(可能)Safari`navigator.appName =="Netscape"`评估为true. (4认同)
  • 同上。在Chrome,Opera,Firefox,Safari上失败 (2认同)

Chr*_*row 25

// detect IE8 and above, and Edge
if (document.documentMode || /Edge/.test(navigator.userAgent)) {
    ... do something
}
Run Code Online (Sandbox Code Playgroud)

说明:

document.documentMode
Run Code Online (Sandbox Code Playgroud)

仅限IE的属性,首先在IE8中可用.

/Edge/
Run Code Online (Sandbox Code Playgroud)

一个正则表达式,用于搜索字符串'Edge' - 然后我们根据'navigator.userAgent'属性进行测试

  • 这是此页面中唯一一个在 IE 和 Edge 上都对我有用的页面。 (2认同)

小智 14

我正在使用UAParser https://github.com/faisalman/ua-parser-js

var a = new UAParser();
var name = a.getResult().browser.name;
var version = a.getResult().browser.version;
Run Code Online (Sandbox Code Playgroud)

  • 通常,特征检测优于浏览器检测.也就是说,如果你真的需要检测浏览器,我会使用一个库(如此处所示).解析用户代理字符串有很多复杂性,库可以降低错误的风险. (2认同)

Gav*_*oni 11

主题有点旧,但由于这里的脚本将Firefox视为误报(EDGE v12),这是我使用的版本:

function isIEorEDGE(){
  if (navigator.appName == 'Microsoft Internet Explorer'){
    return true; // IE
  }
  else if(navigator.appName == "Netscape"){                       
     return navigator.appVersion.indexOf('Edge') > -1; // EDGE
  }       

  return false;
}
Run Code Online (Sandbox Code Playgroud)

当然可以用更简洁的方式编写:

function isIEorEDGE(){
  return navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.appVersion.indexOf('Edge') > -1);
}
Run Code Online (Sandbox Code Playgroud)

  • IE 11失败.IE11只提供'Netscape'作为appname,从而使条件失败 (4认同)

pat*_*hie 6

如果您只想向使用MS浏览器的用户发出警告或其他提示,则此代码应该不错。

HTML:

<p id="IE">You are not using a microsoft browser</p>
Run Code Online (Sandbox Code Playgroud)

Javascript:

using_ms_browser = navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.appVersion.indexOf('Edge') > -1) || (navigator.appName == "Netscape" && navigator.appVersion.indexOf('Trident') > -1);

if (using_ms_browser == true){
    document.getElementById('IE').innerHTML = "You are using a MS browser"
}
Run Code Online (Sandbox Code Playgroud)

感谢@GavinoGrifoni


pho*_*cks 6

此功能对我来说非常有效。它也检测Edge。

最初来自此Codepen:

https://codepen.io/gapcode/pen/vEJNZN

/**
 * detect IE
 * returns version of IE or false, if browser is not Internet Explorer
 */
function detectIE() {
  var ua = window.navigator.userAgent;

  // Test values; Uncomment to check result …

  // IE 10
  // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';

  // IE 11
  // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';

  // Edge 12 (Spartan)
  // ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';

  // Edge 13
  // ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';

  var msie = ua.indexOf('MSIE ');
  if (msie > 0) {
    // IE 10 or older => return version number
    return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  }

  var trident = ua.indexOf('Trident/');
  if (trident > 0) {
    // IE 11 => return version number
    var rv = ua.indexOf('rv:');
    return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  }

  var edge = ua.indexOf('Edge/');
  if (edge > 0) {
    // Edge (IE 12+) => return version number
    return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  }

  // other browser
  return false;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以if (detectIE()) { /* do IE stuff */ }在代码中使用。