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 It self.

is there a problem with the code? or it's some bug?

Christian Eric Paran
  • 980
  • 6
  • 27
  • 49
  • 1
    Best Solution: Just code in such a way that you don't need to "browser sniff" – JakeParis May 08 '12 at 20:28
  • why don't you feature detect? – Daniel A. White May 08 '12 at 20:28
  • Why are you trying to sniff the browser if I may ask? – PeeHaa May 08 '12 at 20:28
  • dont knwo about js, do you want it in php though? – Semur Nabiev May 08 '12 at 20:29
  • 3
    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?](http://stackoverflow.com/q/9847580/938089?how-to-detect-safari-chrome-ie-firefox-and-opera-browser) – Rob W May 08 '12 at 20:31
  • 1
    +1 for feature detection. Read the Q&A here: http://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection – David Gorsline May 08 '12 at 20:35
  • 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. – evianpring Oct 24 '17 at 09:23
  • @JakeParis "How do we cure AIDS?" "Best Solution: Just don't get AIDS." – Kröw Jun 11 '22 at 00:25

6 Answers6

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)
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • 1
    http://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? –  May 30 '16 at 09:33
4

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";
  }
}
  • 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? –  May 30 '16 at 09:37
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

JakeParis
  • 11,056
  • 3
  • 42
  • 65
0

Try navigator.appVersion, it should be more specific.

Bakudan
  • 19,134
  • 9
  • 53
  • 73
Lyn Headley
  • 11,368
  • 3
  • 33
  • 35
0

Here's a link to really good js file that should answer all your questions:

http://www.quirksmode.org/js/detect.html

empire1138
  • 11
  • 1
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.

Community
  • 1
  • 1
Bakudan
  • 19,134
  • 9
  • 53
  • 73