How can I determine what browser is being used with JavaScript?
Asked Answered
L

7

7

I have a problem in determining browsers.

I've tried using navigator and well, it did not help.

I used alert(navigator.appName); to determine the browser and I'm currently using Google Chrome, when the pop up appears it displayed Mozilla, but in Mozilla it works fine and with Mozilla itself.

Is there a problem with the code, or is it a bug?

Lanugo answered 8/5, 2012 at 20:26 Comment(8)
Best Solution: Just code in such a way that you don't need to "browser sniff"Potsherd
why don't you feature detect?Irmine
Why are you trying to sniff the browser if I may ask?Fanni
dont knwo about js, do you want it in php though?Amabil
Using User agent strings for detecting browsers is very unreliable, as this string can easily be changed. I recommend to check for vendor-specific values. A recent creation was posted here: How to detect Safari, Chrome, IE, Firefox and Opera browser?Alsatia
+1 for feature detection. Read the Q&A here: #1295086Podvin
Agree that feature detection is ideal. However, maybe this is a case where it can't be used: I need to make a webrtc call between Chrome and Firefox for an app prototype. Apparently Firefox has some issue with TURN servers that is not solved even with a standard shim file. Between the delivery of the prototype and me actually looking in to the issue to even detect what the feature is that's missing, I need to make sure users of the prototype use Chrome. Therefore, it would be nice to have an easy way to detect non-Chrome.Deductive
@Potsherd "How do we cure AIDS?" "Best Solution: Just don't get AIDS."Erogenous
P
2

To answer your question, no there is no problem or bug. Chrome represents itself as Mozilla. See this for the exact User Agent strings which Chrome gives.

http://www.useragentstring.com/pages/useragentstring.php?name=Chrome

Here are some examples:

Chrome 20.0.1092.0

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6

Chrome 20.0.1090.0

Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6

Potsherd answered 8/5, 2012 at 20:29 Comment(1)
@Mox I Fixed it.Potsherd
S
6
navigator.sayswho= (function(){
    var N= navigator.appName, ua= navigator.userAgent, tem,
    M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*([\d\.]+)/i);
    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M= M? [M[1], M[2]]:[N, navigator.appVersion, '-?'];
    return M.join(' ');
})();

alert(navigator.sayswho)
Sycophancy answered 8/5, 2012 at 20:51 Comment(1)
imgur.com/a/fNWJE - please see the output of IE in first screen shot, Safari in second screen shot. Can you please make the script little more updated. So that it can return true or false, if browser is SAFARI or IE of any versions?Lindemann
D
5

It's close to chrome, if you need a simple short solution try to use this:

function getBrowser() {
  if( navigator.userAgent.indexOf("Chrome") != -1 ) {
    return "Chrome";
  } else if( navigator.userAgent.indexOf("Opera") != -1 ) {
    return "Opera";
  } else if( navigator.userAgent.indexOf("MSIE") != -1 ) {
    return "IE";
  } else if( navigator.userAgent.indexOf("Firefox") != -1 ) {
    return "Firefox";
  } else {
    return "unknown";
  }
}
Dendrochronology answered 8/5, 2012 at 20:38 Comment(1)
Can you make sure if your script also working if its used in Android or iPhone or Windows Phone or other Linux phone browsers?Lindemann
P
2

To answer your question, no there is no problem or bug. Chrome represents itself as Mozilla. See this for the exact User Agent strings which Chrome gives.

http://www.useragentstring.com/pages/useragentstring.php?name=Chrome

Here are some examples:

Chrome 20.0.1092.0

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6

Chrome 20.0.1090.0

Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6

Potsherd answered 8/5, 2012 at 20:29 Comment(1)
@Mox I Fixed it.Potsherd
P
0

Try navigator.appVersion, it should be more specific.

Progeny answered 8/5, 2012 at 20:29 Comment(0)
M
0

Here's a really good article that should answer all your questions.

Merri answered 8/5, 2012 at 20:31 Comment(0)
S
0

The browser sniffing wikipedia MDC is not considered a good practice. What if there is new browser, not publically available? The detection should be towards features not browsers. Browsers may change, became outdated, features are persistent.

Just for the of completeness and the spirit of adventure - there is a way to test for specific JavaScript object:

isChrome = function() { return !!(window.chrome);}
isOpera = function() { return !!(window.opera);}

For IE there is this magic thingy called conditional compilation SO Question and materials about it MSDN JSkit.

Saul answered 8/5, 2012 at 23:6 Comment(0)
G
0
navigator.userAgentData.brands[0].brand

This will give you the browser name.

Griego answered 1/10, 2024 at 15:25 Comment(2)
Well, might. It's experimental. Not in Firefox or SafariBoeotian
My bad, I just checked with chrome, brave and edge.Griego

© 2022 - 2025 — McMap. All rights reserved.