Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target Edge browser with javascript

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.

like image 918
Willem de Wit Avatar asked Jul 30 '15 10:07

Willem de Wit


People also ask

Can Microsoft Edge run JavaScript?

Edge (Windows 10)JavaScript is supported in the Microsoft Edge web browser. However, it might have been disabled in your browser by an administrative setting. If you encounter a JavaScript error in Edge: On the More menu (...), select Open with Internet Explorer.

What engine does Microsoft Edge browser use to run JavaScript code?

Microsoft, in true maverick fashion, built its Edge browser with its own EdgeHTML browser engine and Chakra JavaScript Engine. With the Edge 79 release, Microsoft is switching to Blink browser engine with V8 JavaScript engine.


3 Answers

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/42.0.2311.135 Safari/537.36 Edge/12.10136

window.navigator.userAgent.indexOf("Edge") > -1
like image 138
sandstrom Avatar answered Oct 05 '22 01:10

sandstrom


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

like image 23
Manoj Kadolkar Avatar answered Oct 05 '22 03:10

Manoj Kadolkar


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...)

like image 7
David Perfors Avatar answered Oct 05 '22 03:10

David Perfors