J. Doe doe doe doe J. Doe doe doe doe - 5 months ago 16
Javascript Question

I am trying to make a function that determines the browser but the new Microsoft Edge is not being detected

I am trying to set a function to determine the browser that the user is currently browsing on. I am dropping support for IE due to the dynamics of the web application. With the Microsoft Edge returning the value of 'Netscape' which is also returned by Firefox I cannot pick it out as easy as IE. Here is my code

(function() { //Check which browser they are using - then let them know if its supported - if not they will be redirected to another page if so they will remain
var ie = (/trident/i).test(navigator.userAgent);
if (ie)
{
console.log("IE is not supported");
}
else
{
if (navigator.appName === 'Netscape')
{
if (navigator.userAgent.indexOf("Firefox"))
{
console.log("Your browser is supported");
}
else
{
console.log("Your browser is not supported");
}
}
else
{
console.log("Your browser is supported");
}
}
})();


I want to keep Firefox but I want to let me know Edge is not supported either. How can I fix this? Everything works fine, except one thing. Edge always logs supported. I want it to be logged that it cannot be used.

Answer

It looks like the full user agent string for desktop is:

Mozilla/5.0 (Windows NT 10.0; <64-bit tags>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev> Edge/<EdgeHTML Rev>.<Windows Build>

This is according to Microsoft's documentation: https://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx

So, I suppose you just have to search for the substring 'Edge/', and you'll have your answer. I didn't test this, but it seems reasonable to me. Something like this:

var isEdge = window.navigator.userAgent.indexOf('Edge/') ? true : false;

Hope this helps.