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

I have looked around a lot, and I understand that there are a lot of ways to detect internet explorer. My problem is this: I have an area on my HTML doc, that when clicked, calls a javascript function that is incompatible with internet explorer of any kind. I wish to detect if IE is being used, and if so, set a variable as 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 to detect IE wok. I think the problem is that I am running it out of notepad++. I need to be able to detect IE, so that based on the variable, I can disable that area of the site. I have tried this:

/* Write JavaScript here */
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++? that's what I'm testing the HTML out of, but I need a method that'll work with that and on a website. HELP!

edit

I notice someone marked this as a duplicate, and that is understandable. I suppose I wasnt clear. I cannot use a JQuery answer, so this is not a duplicate as I am asking for a vanilla JS answer.

Edit #2 is there also a way to detect the edge browser?


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.

Instead of looking at the navigator.userAgent, I looked at navigator.appName to distinguish if it was IE<=10 or IE11 and Edge. IE11 and Edge use the appName of "Netscape", while every other iteration uses "Microsoft Internet Explorer".

After we determine that the browser is either IE11 or Edge, I then looked to navigator.appVersion. I noticed that in IE11 the string was rather long with a lot of information inside of it. I arbitrarily picked out the word "Trident", which is definitely not in the navigator.appVersion for Edge. 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. If on Microsoft Edge it returns the number 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;          
}

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

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


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

var a = new UAParser();
var name = a.getResult().browser.name;
var version = a.getResult().browser.version;
链接地址: http://www.djcxy.com/p/36450.html

上一篇: 通过PHP / Perl在用户浏览器中显示PDF文件

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