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.
Browser checks using javascript
IsChrome: boolean = window.navigator.userAgent.indexOf("Chrome") > -1;
IsIE: boolean = window.navigator.userAgent.indexOf("MSIE ") > -1 || !!navigator.userAgent.match(/Trident.*rv:11./);
IsEdge: boolean = window.navigator.userAgent.indexOf("Edge/") > -1;
IsSafari: boolean = window.navigator.userAgent.indexOf("Safari") > -1 && window.navigator.userAgent.indexOf('Chrome') == -1;
IsFirefox: boolean = window.navigator.userAgent.indexOf("Firefox") > -1;
IsChromiumEdge: boolean = window.navigator.userAgent.indexOf("Edg/") > -1; // for new edge chromium
Everyone seems to be saying the same thing here, except no one has provided a solid 1 liner solution.
So from this answer, https://stackoverflow.com/a/32107845/584147
Which simply says that,
I have turned this into a simple 1 liner for anyone else who ends up here and in need of it.
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
Raw approach with Javascript
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:
(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 EdgeYou 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.
https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx
Have a try with:
Replace the String
Microsoft Internet Explorer
with maybe something ofEdge
or similar.EDIT: You can find out what the user agent string is with: