devjs11
devjs11

Reputation: 1958

Chrome is detected as Safari with javascript

I am looking for a way to detect Safari with javascript. I know its been covered many times already but probably something got changed and it does not work anymore. At least in my case.

Here is what I do:

<script>

    if(!isSafari()){
      alert('not Safari');
    } else {
      alert('I am Safari');
    }


    function isSafari(){    
      var is_safari = navigator.userAgent.indexOf("Safari") > -1;
      if(is_safari){
        return true;
      }
    }
  </script>

jsbin: http://jsbin.com/ewerof/1

If you run this code in Safari and Chrome you will get the same alert "I am Safari" So how to actually detect Safari only? My Safari version is 4.0.3 just in case if that matters.

Upvotes: 5

Views: 10406

Answers (4)

DannyTheDev
DannyTheDev

Reputation: 4173

If you look at your user agent you'll see it includes "Safari" in the string;

Your User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.92 Safari/537.4

var xSAF = isSafari();

if(SAF)
{
    alert('Is Safari');
} else {
    alert('Not Safari');
}

function isSafari()
{
    var xUA = navigator.userAgent;
    if((xUA.indexOf("Safari")) && (xUA.indexOf("Chrome") == -1))
    {
        return true;
    } else {
        return false;
    }
}

Upvotes: 0

David Hellsing
David Hellsing

Reputation: 108510

If you type this in a web developer console using Chrome:

navigator.userAgent

You will get a string, something like:

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.92 Safari/537.4"

This string contains Safari, so you have to check specifically if the string also contains chrome. You can use a simple one-liner for that:

var is_safari = /^(?!.*chrome).*safari/i.test(navigator.userAgent);

Upvotes: 2

KTastrophy
KTastrophy

Reputation: 1739

I'm using a newer version of Safari but this should still apply.

Safari's User Agent:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14

Chrome's User Agent

 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4

As you can see they both contain "Safari" (Most likely due to them both using WebKit). Your function only checks for Safari. So your function needs to check for "Safari" and make sure the string doesn't contain "Chrome".

Upvotes: 0

mrd
mrd

Reputation: 2183

Chrome has both 'Chrome' and 'Safari' inside userAgent string. Safari has only 'Safari'.

So this works:

var is_chrome = navigator.userAgent.indexOf('Chrome') > -1; 
var is_explorer = navigator.userAgent.indexOf('MSIE') > -1; 
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1; 
var is_safari = navigator.userAgent.indexOf("Safari") > -1; 
var is_Opera = navigator.userAgent.indexOf("Presto") > -1; 
if ((is_chrome)&&(is_safari)) {is_safari=false;} 

if (is_safari) alert('Safari');

Or for Safari only, use this :

if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) {alert('Its Safari');} 

Credit: Kabamaru

Upvotes: 14

Related Questions