How to target Edge browser with javascript
Asked Answered
G

11

48

I know you should do feature detection where possible, but can you detect in Javascript if the browser is the Microsoft Edge browser?

I maintain an old product and I want to display a warning that some features could be broken without having to invest a lot of time fixing the old code.

Glorygloryofthesnow answered 30/7, 2015 at 10:26 Comment(9)
What features are you concerned about Edge supporting?Jaclynjaco
You can user agent sniff, but individual browser detecting is bad practice. Instead detect for features.Ziegler
see #30592206 and as previously stated, agent sniffing is a bad idea.Spancake
Possible duplicate of (#30592206)Ziegler
For example old versions of Tibico General Interface (JSX30.js) don't support edge. So as a workaround it is quite important to identify the browser. Patching an old JSX version is very expensive (time), updating (assuming new versions work) is very, very expensive (time).Winther
possible duplicate of Detecting Microsoft's edge or spartan with javascriptWinther
You can use this generic detection code, and target browser=='ed' for Edge - #21757605Anglicism
It should be noted that the all new Edge has changed its user-agent to reflect the fact that it is no longer using the Microsoft engine under the hood. The user-agent is now: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36 Edg/80.0.361.109. However, I don't think that an additional check for the missing e is necessary, as Edge is now based upon Chromium and basically the same as Google Chrome from now on - effectively eliminating incompatibilities.Vagary
For the record, the ARE differences still with Edge, such as AV1 support which while the js engine is the same, the actual capabilities are not.Sarcenet
L
60

Try to detect features instead of a specific browser. It's more future-proof. Only rarely should you use browser detection.

With that out of the way: one option is to use a library (there are many intricacies to User Agent strings), or alternatively to parse window.navigator.userAgent manually.

Using a parser library

# https://github.com/faisalman/ua-parser-js.

var parser = new UAParser();
var result = parser.getResult();

var name = result.browser.name;
var version = result.browser.version;

Raw approach with Javascript

# Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) \
# Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26

window.navigator.userAgent.indexOf("Edg") > -1
Losel answered 4/10, 2015 at 21:4 Comment(3)
Note some versions of Edge report user agent as Edg (missing last "e"), for whatever reason.Enrichment
Edg is for the new Chromium based Edge, that should be detected as Chrome for maximum compatibility.Creationism
for edge version 83 is deprecatedJorry
P
29

Here's the simple script to detect Edge browser

if (/Edge/.test(navigator.userAgent)) {
    alert('Hello Microsoft User!');
}

Explanation:

/Edge/

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

Plunder answered 25/10, 2017 at 5:1 Comment(5)
for edge version 83 is deprecatedJorry
@OmerCohen MS Edge Anaheim is based on chromium engine and the above code was written for Edge legacy...Plunder
yes this works for users with ruby < 2.5.0 if they are stuck with Browser gemMadura
Thanks works with Trident too :)Keelin
2023: /Edg/.test(navigator.userAgent)Formerly
S
7

The useragent string contains Edge/12.9600, where the 12.9600 is the version number I tested with. This is completely different from the user agent string of Internet Explorer in 'Edge' mode.

User agent string of Edge:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600

User agent string of IE10 in Edge mode:

Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0; rv:11.0) like Gecko

So when using javascript, just check for the word 'Edge' in the user agent string. When you also test for other browsers, make sure you check Edge first, otherwise you will get false positives (for example Chrome or Safari...)

Soso answered 30/7, 2015 at 11:35 Comment(0)
M
6
navigator.appVersion.indexOf('Edge') > -1
Molar answered 7/12, 2015 at 20:35 Comment(4)
can you please explain how it works and also how it is better than other solutions?Intraatomic
if (/Edge/.test(navigator.userAgent)) { var orientangle = "n/a!" } else {var orientangle = screen.orientation.angle}Quintain
for edge version 83 is deprecatedJorry
I the newer version they call it "Edg" instead of "Edge" navigator.appVersion.indexOf("Edg") > -1Verminous
E
6

I feel this is the best approach, as approaches above were buggy or didn't work properly.

if (/Edg/.test(navigator.userAgent)) {
  alert("You are using Edge.");
}

(function getBrowser() {
  //checks for individual browsers
  let chromeCheck =
    /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  let firefoxCheck = /Firefox/.test(navigator.userAgent);
  let ieCheck = /Edg/.test(navigator.userAgent);

  //if/else to check browsers against defined variables
  if (chromeCheck && !ieCheck) {
    isBrowser.textContent = `Yes. It's Chrome!`;
  } else if (firefoxCheck) {
    isBrowser.textContent = `No. This is Firefox.`;
  } else if (ieCheck) {
    isBrowser.textContent = `No. It's Edge.`;
  } else {
    isBrowser.textContent = `We have no clue!`;
  }
})();
Is it Chrome or something else? <span id="isBrowser"></span><br>
(Open this link in Edge to verify)
Excrete answered 5/3, 2021 at 4:43 Comment(0)
A
4

You are not alone with this problem. At time of writing this post, even the Google Analytics does not exclusively identify the Edge browser.

Your best bet is to refer to the user agent of the request, it will be something like this:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600

(To extract the user agent from the request in JavaScript see this post: Getting the User Agent with JavaScript)

Edge is only available in Windows 10 so you can use this knowledge to help ensure your logic is safe. Look for the following values in the UA:

  • Windows NT 10.0 - which tells you that the user is on Windows 10)
  • Edge - which tells you that the user is on Edge

You could, of course, just look for Edge too.

Update - 05/08

Google Analytics have now included Windows 10 and Edge as first class dimensions and these can both now be filtered directly.

Appeasement answered 5/8, 2015 at 6:21 Comment(1)
for Edge version 83 is depreacatedJorry
S
4

Browser checks using javascript

IsChrome: window.navigator.userAgent.indexOf("Chrome") > -1;

IsIE: window.navigator.userAgent.indexOf("MSIE ") > -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./);

IsEdge: window.navigator.userAgent.indexOf("Edge/") > -1;

IsSafari: window.navigator.userAgent.indexOf("Safari") > -1 && window.navigator.userAgent.indexOf('Chrome') == -1;

IsFirefox: window.navigator.userAgent.indexOf("Firefox") > -1;

IsChromiumEdge: window.navigator.userAgent.indexOf("Edg/") > -1; // for new edge chromium

Science answered 20/1, 2020 at 7:45 Comment(2)
for edge version 83 is deprecatedJorry
Edge detection doesn't work on Edge 119.Pentobarbital
F
2

The snippet that I have here is also copy from SO too, am sorry that I could not give you reference for the original code, but I have modified it so here it is for those of you looking for snippet to check for IE 11 and MS Edge Window 10:

var get_ie_version = function () {
    var sAgent = window.navigator.userAgent;
    var Idx = sAgent.indexOf("MSIE");

    // If IE, return version number.
    if (Idx > 0) {
        return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));
    }
    // Condition Check IF IE 11 and or MS Edge
    else if ( !!navigator.userAgent.match(/Trident\/7\./)
        || window.navigator.userAgent.indexOf("Edge") > -1 )
    {
        return 11;
    } else {
        return 0; //It is not IE
    }
};
Flew answered 12/7, 2016 at 8:27 Comment(1)
for edge version 83 is deprecatedJorry
X
1

Edge version 90 doesn't shows the edg/ part on useragent. However it shows windows and safari. Modern Safari doesn't run on Windows, so I made this validation which checks for windows and safari on user agent, and it works

var isEdge = (function() {
  var agent = window.navigator.userAgent.toLowerCase();
  return agent.indexOf("windows") > -1 && agent.indexOf("safari") > -1
    ? true
    : false;
})();
Xeres answered 1/6, 2021 at 21:36 Comment(0)
U
0

https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx

Have a try with:

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}

Replace the String Microsoft Internet Explorer with maybe something of Edge or similar.

EDIT: You can find out what the user agent string is with:

alert(navigator.userAgent)
Unadvised answered 30/7, 2015 at 10:31 Comment(5)
Wouldn't this fail? Edge's user agent string looks like this, Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136. There's no MSIE in sight. (https://mcmap.net/q/55542/-what-is-the-user-agent-string-name-for-microsoft-edge)Ziegler
But the userAgent String contains Windows NT 10 and Edge/12.... Which both could be used to get the Browsers InfoUnadvised
That is true. I was focused more on the MSIE, that I forgot you could use a different string to find...Ziegler
Unfortunately that wasn't me. I don't downvote unless something's seriously wrong.Ziegler
You were being helpful. Although whoever downvoted probably thought the question shouldn't have had an answer which helped browser sniffing. I upvoted to counter the downvote.Ziegler
T
-4

Everyone seems to be saying the same thing here, except no one has provided a solid 1 liner solution.

So from this answer, https://mcmap.net/q/41995/-how-can-i-detect-internet-explorer-ie-and-microsoft-edge-using-javascript

Which simply says that,

  1. Both of these browsers (IE 11 & Edge) use the navigator.appName of "Netscape" (compared to older versions of IE which use "Microsoft Internet Explorer" as the navigator.appName)
  2. in IE 11 the navigator.appVersion says 'trident', in Edge the navigator.appVersion does not say trident

I have turned this into a simple 1 liner for anyone else who ends up here and in need of it.

var isieEdge = (navigator.appName == "Netscape") && (navigator.appVersion.indexOf('Trident') === -1); // IE Edge

alert(isieEdge);
Thomsen answered 3/12, 2015 at 17:58 Comment(3)
Interesting, I had a different check for chrome which probably happened before this one, so I did not test cross browser with this. Thanks for the heads up, maybe when I get some time & when I get back to that project, ill consider updating this.Thomsen
The detection code I currently use in production: #21757605Anglicism
This code doesn't detect any browser. Try appName for other browsers including old versions, present in 2015.Heida

© 2022 - 2024 — McMap. All rights reserved.