How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?
Solution 1:
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
Solution 2:
// 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
}
Solution 3:
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;
}
Solution 4:
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;
Solution 5:
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);
}