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

13

84

I've looked around a lot, and I understand that there's a lot of ways to detect 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. I want to detect if IE is being used, and if so, set the variable to 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. 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:

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.

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.

Edit #2

Is there also a way to detect the Microsoft Edge browser?

Difficile answered 1/8, 2015 at 3:17 Comment(5)
It will be much easier for you to make the code work on IE and other browser instead of writing IE specific code.Equuleus
If you can't just fix your function to work in IE, then feature detection is considered by most to be a much, much better way to write code than browser detection. Plus, it is much better at forward compatibility as browsers change. For example, do you already know what you want to do with Edge, Microsoft's newest browser.La
possible duplicate of jQuery: check if user is using IE check the link, there are many solutionsRadiobroadcast
If one of the answers below answered your question, the way this site works works, you'd "accept" the answer, more here: What should I do when someone answers my question?. But only if your question really has been answered. If not, consider adding more details to the question.Slow
Added edge detection to my answer and also a handy link where you can see the latest versions.Niphablepsia
K
31

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;          
}
Kroll answered 20/8, 2015 at 0:45 Comment(4)
This is returning 12 in both edge and firefox.Pew
@Pew I've posted an answer below which takes care of this issue. https://mcmap.net/q/41995/-how-can-i-detect-internet-explorer-ie-and-microsoft-edge-using-javascriptRicarda
This does not work. In Chrome, Opera, Firefox, and (probably) Safari navigator.appName == "Netscape" evaluates to true.Foreclose
DITTO. Fails on Chrome, Opera, Firefox, SafariGow
N
94

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

Niphablepsia answered 1/8, 2015 at 3:42 Comment(10)
FWIW, when I check navigator.userAgent in my Win10/Edge VM I only get the first part of it, which doesn't include the "Edge" string.Orestes
Wich version of Edge are you using and could you post it here?Niphablepsia
I'm using VirtualBox 5.0.2 r 102096 and the image is named Microsoft Edge.Win10.For.Mac.VirtualBox.zip downloaded from the MS site on Monday 27 Aug 2015. The image desktop background says Build 10240, the Edge home page says I'm on the "most recent build of this VM, 20150801", shown in Settings as 20.10240.16384.0.Orestes
Thanks and the navigator.userAgent string?Niphablepsia
It's being returned as "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" instead of the full string. IIRC I found another SO question regarding the VM/image issue.Orestes
You could report the bug to Microsoft. The userAgent string should be build according to their own guideline.Niphablepsia
Yes, I understand that; AFAICT they're aware of the problem on this specific VM/Image.Orestes
@DaveNewton is it confirmed that the user agent string bug you are seeing only happens on the VM/Image? JW.Nahuatlan
@Nahuatlan Don't know; it was the only place we've seen it here, but this was awhile ago now.Orestes
if only you could wrap it in a methodBookman
T
38
// 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.

/Edge/

A regular expression to search for the string 'Edge' - which we then test against the 'navigator.userAgent' property

Update Mar 2020

@Jam comments that the latest version of Edge now reports Edg as the user agent. So the check would be:

if (document.documentMode || /Edge/.test(navigator.userAgent) || /Edg/.test(navigator.userAgent)) {
    ... do something
}
Tamqrah answered 18/10, 2017 at 21:52 Comment(4)
This is the only one of this page working for me both on IE and Edge.Fiscus
/Edge\//.test(navigator.userAgent) To bre more robust, I prefer to include the backslash.Forbearance
In the latest version of Edge, it's just Edg in the user agent. So to test for IE, Edge and Edge chromium, and including the slash: document.documentMode || /Edge\//.test(navigator.userAgent) || /Edg\//.test(navigator.userAgent)Absolute
I suppose you don't need to detect Edge with "Edg" UA string, because it's Chromium-based and probably not wanted to be detected (as opposed to EdgeHTML)Amaranthaceous
K
31

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;          
}
Kroll answered 20/8, 2015 at 0:45 Comment(4)
This is returning 12 in both edge and firefox.Pew
@Pew I've posted an answer below which takes care of this issue. https://mcmap.net/q/41995/-how-can-i-detect-internet-explorer-ie-and-microsoft-edge-using-javascriptRicarda
This does not work. In Chrome, Opera, Firefox, and (probably) Safari navigator.appName == "Netscape" evaluates to true.Foreclose
DITTO. Fails on Chrome, Opera, Firefox, SafariGow
Q
15

I'm using UAParser https://github.com/faisalman/ua-parser-js

var a = new UAParser();
var name = a.getResult().browser.name;
var version = a.getResult().browser.version;
Quarry answered 23/9, 2015 at 15:20 Comment(2)
In general, feature detection is better than browser detection. That said, if you really need to detect a browser I'd use a library (as suggested here). There are a lot of intricacies to parsing user agent strings, a library will lower the risk of a mistake.Bobseine
Very helpful, none of the other solutions worked, this plugin worked perfectly for me.Pew
R
11

Topic is a bit old, but since the scripts here detect Firefox as a False Positive (EDGE v12), here is the version I use:

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);
}
Ricarda answered 18/4, 2016 at 8:23 Comment(8)
returns false for edgePradeep
@CreamWhippedAirplane can you post here the result of navigator.appName given by your version of Edge? Maybe they changed it recentlyRicarda
@CreamWhippedAirplane seems they changed navigator.appVersion: now it no longer contains "Trident" but instead they properly add "Edge" to the name. Updating my answer right now!Ricarda
This didn't work for me when testing on browserstack.com, I do not know if it works on a real machine! But on browserstack I got Netscape as appName...Hog
Fails for IE 11. IE 11 gives only 'Netscape' as appname thus failing the conditionJester
@AnkurAggarwal any idea on how to discriminate it from firefox? maybe IE 11 still has "Trident" in navigator.appVersion as the early Edge?Ricarda
@Ricarda tested below Reinout van Kempen answer using user agent. It works fine on edge as well as ie alsoJester
(navigator.appName == "Netscape" && (navigator.appVersion.indexOf('Edge') || navigator.appVersion.indexOf('Trident')) > -1Stringer
S
8

This function worked perfectly for me. It detects Edge as well.

Originally from this 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;
}

Then you can use if (detectIE()) { /* do IE stuff */ } in your code.

Screenplay answered 25/9, 2017 at 23:14 Comment(1)
This worked for me in 2019. Other solutions on this page gave false positive for Firefox, or even false negative for IE 11.Titanic
T
6

If you just want to give users using a MS browser a warning or something, this code should be good.

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

Thanks to @GavinoGrifoni

Tinct answered 13/9, 2017 at 9:53 Comment(0)
A
3

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/

Anticathexis answered 27/5, 2017 at 17:21 Comment(0)
R
2

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

Rozanne answered 7/3, 2017 at 15:1 Comment(1)
This worked best for me, although the ternary op is redundant. navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1 evaluates to true or false so you can just return/use that.Misogynist
I
2

One line code to detect the browser.

If the browser is IE or Edge, It will return true;

let isIE = /edge|msie\s|trident\//i.test(window.navigator.userAgent)
Impudicity answered 19/9, 2019 at 6:17 Comment(0)
R
0

Here is a javascript class that detects IE10, IE11 and Edge.
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();
};
Rumilly answered 27/4, 2017 at 13:42 Comment(0)
B
0

If we need to check Edge please go head with this

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

//do something

}

Brim answered 13/3, 2020 at 7:45 Comment(0)
D
-4

First of all its not the Notepad++ problem for sure. 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/

if(navigator.userAgent.indexOf("MSIE") != -1){
   alert('I am Internet Explorer!!');
}
Dedans answered 1/8, 2015 at 11:47 Comment(2)
tried this, it does not work. i ran the code from notepad++ in IE, and there was no alert. I checked the debugger console and there were no errors. I am running windows 7, not sure if that affects version of IEDifficile
What is the IE version you are using ?Dedans

© 2022 - 2024 — McMap. All rights reserved.