简体   繁体   English

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

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

I've looked around a lot, and I understand that there's a lot of ways to detect internet explorer.我环顾四周,我知道有很多方法可以检测 Internet Explorer。

My problem is this: I have an area on my HTML document, that when clicked, calls a JavaScript function that is incompatible with internet explorer of any kind.我的问题是:我的 HTML 文档上有一个区域,单击该区域时会调用与任何类型的 Internet Explorer 不兼容的 JavaScript 函数。 I want to detect if IE is being used, and if so, set the variable to true.我想检测是否正在使用 IE,如果是,请将变量设置为 true。

The problem is, I am writing my code out of Notepad++, and when I run the HTML code in browser, none of the methods for detecting IE work.问题是,我正在用 Notepad++ 编写代码,当我在浏览器中运行 HTML 代码时,检测 IE 的方法都不起作用。 I think the problem is that I am running it out of Notepad++.我认为问题是我正在用 Notepad++ 运行它。 I need to be able to detect IE, so that based on the variable, I can disable that area of the site.我需要能够检测 IE,以便根据变量,我可以禁用该站点的该区域。 I have tried this:我试过这个:

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';
    }
}

but it doesn't work.但它不起作用。 How can I detect IE out of Notepad++?如何从 Notepad++ 中检测 IE? That's what I'm testing the HTML out of, but I need a method that'll work with that.这就是我用来测试 HTML 的内容,但我需要一种可以处理它的方法。

edit编辑

I noticed someone has marked this as a duplicate, and that is understandable.我注意到有人将此标记为重复,这是可以理解的。 I suppose I was not clear.我想我没有说清楚。 I cannot use a JQuery answer, so this is not a duplicate as I am asking for a vanilla JS answer.我不能使用 JQuery 答案,所以这不是重复的,因为我要求的是普通的 JS 答案。

Edit #2编辑 #2

Is there also a way to detect the Microsoft Edge browser?还有一种方法可以检测 Microsoft Edge 浏览器吗?

Here is the latest correct way that I know of how to check for IE and 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');
}

Note that you don't need the extra var isIE10 in your code because it does very specific checks now.请注意,您的代码中不需要额外的 var isIE10,因为它现在会进行非常具体的检查。

Also check out this page for the latest IE and Edge user agent strings because this answer may become outdated at some point: https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx另请查看此页面以获取最新的 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
}

Explanation:解释:

document.documentMode

An IE only property, first available in IE8.一个仅限 IE 的属性,首先在 IE8 中可用。

/Edge/

A regular expression to search for the string 'Edge' - which we then test against the 'navigator.userAgent' property用于搜索字符串“Edge”的正则表达式 - 然后我们针对“navigator.userAgent”属性进行测试

Update Mar 2020 2020 年 3 月更新

@Jam comments that the latest version of Edge now reports Edg as the user agent. @Jam 评论说,最新版本的 Edge 现在将Edg报告为用户代理。 So the check would be:所以检查将是:

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

I don't know why, but I'm not seeing "Edge" in the userAgent like everyone else is talking about, so I had to take another route that may help some people.我不知道为什么,但我没有像其他人谈论的那样在 userAgent 中看到“Edge”,所以我不得不采取另一条可能对某些人有所帮助的路线。

Instead of looking at the navigator.userAgent, I looked at navigator.appName to distinguish if it was IE<=10 or IE11 and Edge.我没有查看 navigator.userAgent,而是查看 navigator.appName 来区分它是 IE<=10 还是 IE11 和 Edge。 IE11 and Edge use the appName of "Netscape", while every other iteration uses "Microsoft Internet Explorer". IE11 和 Edge 使用“Netscape”的 appName,而其他所有迭代都使用“Microsoft Internet Explorer”。

After we determine that the browser is either IE11 or Edge, I then looked to navigator.appVersion.在我们确定浏览器是 IE11 或 Edge 之后,我查看了 navigator.appVersion。 I noticed that in IE11 the string was rather long with a lot of information inside of it.我注意到在 IE11 中,字符串很长,里面有很多信息。 I arbitrarily picked out the word "Trident", which is definitely not in the navigator.appVersion for Edge.我随意挑出了“三叉戟”这个词,Edge 的 navigator.appVersion 中绝对没有这个词。 Testing for this word allowed me to distinguish the two.测试这个词让我能够区分这两者。

Below is a function that will return a numerical value of which Internet Explorer the user is on.下面是一个函数,它将返回用户使用的 Internet Explorer 的数值。 If on Microsoft Edge it returns the number 12.如果在 Microsoft Edge 上,则返回数字 12。

Good luck and I hope this helps!祝你好运,我希望这会有所帮助!

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

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

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

Topic is a bit old, but since the scripts here detect Firefox as a False Positive (EDGE v12), here is the version I use:话题有点老,但由于这里的脚本将 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;
}

which of course can be written in a more concise way:这当然可以用更简洁的方式编写:

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

This function worked perfectly for me.这个功能非常适合我。 It detects Edge as well.它也检测边缘。

Originally from this Codepen:最初来自这个 Codepen:

https://codepen.io/gapcode/pen/vEJNZN 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;
}

Then you can use if (detectIE()) { /* do IE stuff */ } in your code.然后你可以在你的代码中使用if (detectIE()) { /* do IE stuff */ }

If you just want to give users using a MS browser a warning or something, this code should be good.如果你只是想给使用 MS 浏览器的用户一个警告什么的,这段代码应该不错。

HTML: HTML:

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

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

Thanks to @GavinoGrifoni感谢@GavinoGrifoni

For me better this:对我来说更好的是:

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;

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

Use this snip : 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; var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false;

One line code to detect the browser.一行代码检测浏览器。

If the browser is IE or Edge, It will return true;如果浏览器是IE或Edge,则返回true;

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

Here is a javascript class that detects IE10, IE11 and Edge.这是一个检测 IE10、IE11 和 Edge 的 javascript 类。
Navigator object is injected for testing purposes.为测试目的注入导航器对象。

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

If we need to check Edge please go head with this如果我们需要检查 Edge,请继续

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

//do something

} }

First of all its not the Notepad++ problem for sure.首先,它肯定不是 Notepad++ 问题。 Its your " String Matching problem "它是你的“字符串匹配问题

The common string throughout all IE version is MSIE Check out the various userAgent strings at http://www.useragentstring.com/pages/Internet%20Explorer/所有 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.

相关问题 如何使用javascript检测IE / Edge? - How to detect IE/Edge using javascript? 如何从 Typescript ZA2F2ED4F8EBC2CBB4C21A29DC41AB61 和 Microsoft Edge 调用 Javascript function? - How to call a Javascript function from a Typescript class that works with Internet Explorer 11 and Microsoft Edge? Javascript破坏了Internet Explorer中属性的空值-如何让IE忽略它? - Javascript breaking on null value of property in Internet Explorer - How can I get IE to ignore this? 如何检测是否已使用JavaScript安装Internet Explorer插件 - How to detect if an Internet Explorer plugin is already installed using JavaScript 如何通过 Javascript 检测 Internet Explorer 的临时 Internet 文件位置? - How do I detect the temporary Internet Files location of Internet Explorer through Javascript? 如何在IE9中检测浏览器模式是否在“Internet Explorer 9兼容性视图”中? - How do I detect if the Browser mode is in “Internet Explorer 9 compatibility View” in IE9? 在perl中检测浏览器,如何检查浏览器是否为perl中的Internet Explorer(IE) - detect browser in perl,how to check if the browser is internet explorer(IE) in perl 从 javascript(Internet Explorer 和 Edge)更改后如何重绘 SVG - How to redraw SVG after change from javascript (Internet Explorer and Edge) 如何仅使用JavaScript定位Internet Explorer 11? - How can I target only Internet Explorer 11 with JavaScript? 如何在Internet Explorer中安装Lync Addon,并使用Javascript进行检查? - How can I check with Javascript if the Lync Addon is installed in Internet Explorer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM