繁体   English   中英

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

[英]How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?

我环顾四周,我知道有很多方法可以检测 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';
    }
}

但它不起作用。 如何从 Notepad++ 中检测 IE? 这就是我用来测试 HTML 的内容,但我需要一种可以处理它的方法。

编辑

我注意到有人将此标记为重复,这是可以理解的。 我想我没有说清楚。 我不能使用 JQuery 答案,所以这不是重复的,因为我要求的是普通的 JS 答案。

编辑 #2

还有一种方法可以检测 Microsoft Edge 浏览器吗?

这是我知道如何检查 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');
}

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

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

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

解释:

document.documentMode

一个仅限 IE 的属性,首先在 IE8 中可用。

/Edge/

用于搜索字符串“Edge”的正则表达式 - 然后我们针对“navigator.userAgent”属性进行测试

2020 年 3 月更新

@Jam 评论说,最新版本的 Edge 现在将Edg报告为用户代理。 所以检查将是:

if (document.documentMode || /Edge/.test(navigator.userAgent) || /Edg/.test(navigator.userAgent)) {
    ... do something
}

我不知道为什么,但我没有像其他人谈论的那样在 userAgent 中看到“Edge”,所以我不得不采取另一条可能对某些人有所帮助的路线。

我没有查看 navigator.userAgent,而是查看 navigator.appName 来区分它是 IE<=10 还是 IE11 和 Edge。 IE11 和 Edge 使用“Netscape”的 appName,而其他所有迭代都使用“Microsoft Internet Explorer”。

在我们确定浏览器是 IE11 或 Edge 之后,我查看了 navigator.appVersion。 我注意到在 IE11 中,字符串很长,里面有很多信息。 我随意挑出了“三叉戟”这个词,Edge 的 navigator.appVersion 中绝对没有这个词。 测试这个词让我能够区分这两者。

下面是一个函数,它将返回用户使用的 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;          
}

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

var a = new UAParser();
var name = a.getResult().browser.name;
var version = a.getResult().browser.version;

话题有点老,但由于这里的脚本将 Firefox 检测为误报(EDGE v12),这是我使用的版本:

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

  return false;
}

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

function isIEorEDGE(){
  return navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.userAgent.indexOf('.NET') > -1);
}

这个功能非常适合我。 它也检测边缘。

最初来自这个 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;
}

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

如果你只是想给使用 MS 浏览器的用户一个警告什么的,这段代码应该不错。

HTML:

<p id="IE">You are not using a microsoft browser</p>

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

感谢@GavinoGrifoni

对我来说更好的是:

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;

去运行: http : //output.jsbin.com/solicul/1/ o http://jsfiddle.net/Webnewbie/apa1nvu8/

使用这个片段: var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false; var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false;

一行代码检测浏览器。

如果浏览器是IE或Edge,则返回true;

let isIE = /edge|msie\s|trident\//i.test(window.navigator.userAgent)

这是一个检测 IE10、IE11 和 Edge 的 javascript 类。
为测试目的注入导航器对象。

var DeviceHelper = function (_navigator) {
    this.navigator = _navigator || navigator;
};
DeviceHelper.prototype.isIE = function() {
    if(!this.navigator.userAgent) {
        return false;
    }

    var IE10 = Boolean(this.navigator.userAgent.match(/(MSIE)/i)),
        IE11 = Boolean(this.navigator.userAgent.match(/(Trident)/i));
    return IE10 || IE11;
};

DeviceHelper.prototype.isEdge = function() {
    return !!this.navigator.userAgent && this.navigator.userAgent.indexOf("Edge") > -1;
};

DeviceHelper.prototype.isMicrosoftBrowser = function() {
    return this.isEdge() || this.isIE();
};

如果我们需要检查 Edge,请继续

if(navigator.userAgent.indexOf("Edge") > 1 ){

//do something

}

首先,它肯定不是 Notepad++ 问题。 它是你的“字符串匹配问题

所有 IE 版本中的通用字符串是MSIEhttp://www.useragentstring.com/pages/Internet%20Explorer/查看各种 userAgent 字符串

if(navigator.userAgent.indexOf("MSIE") != -1){
   alert('I am Internet Explorer!!');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM