2

I'm using $.browser to try and check what type of browser users have accessing my site. The problem I have however is that both Safari and Chrome are webkit browsers and I therefore do not know how to distinguish between the too.

Is there a way to distinguish between safari and chrome so that I can have my site do something different?

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Cliftwalker
  • 369
  • 1
  • 5
  • 17
  • check this question, it may help. http://stackoverflow.com/questions/3303858/distinguish-chrome-from-safari-using-jquery-browser – reader_1000 Sep 08 '11 at 11:04
  • possible duplicate of [jQuery 1.6 Browser detection](http://stackoverflow.com/questions/6674246/jquery-1-6-browser-detection) – Madara's Ghost Sep 08 '11 at 11:07

3 Answers3

4
function browserTester(browserString) {
    return navigator.userAgent.toLowerCase().indexOf(browserString) > -1;
}

if(browserTester('chrome')) {
    // do stuff for chrome
} else if(browserTester('safari')) {
    //do stuff for safari
}

http://jsfiddle.net/genesis/gm3Na/

genesis
  • 50,477
  • 20
  • 96
  • 125
0

You can use the navigator.userAgent to find this out.

Some sample outputs: Chrome:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1

Firefox:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0.2) Gecko/20100101 Firefox/6.0.2

Safari:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3

See here: http://jsfiddle.net/bQrgB/

Martin Schlagnitweit
  • 2,042
  • 6
  • 26
  • 42
0

Here you have a similar topic: Distinguish Chrome from Safari using jQuery.browser

var userAgent = navigator.userAgent.toLowerCase();
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());

// Is this a version of Chrome?
if($.browser.chrome){
  userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
  $.browser.version = userAgent;
  // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
  $.browser.safari = false;
}

// Is this a version of Safari?
if($.browser.safari){
  userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
  $.browser.version = userAgent;
}
Community
  • 1
  • 1
expertCode
  • 533
  • 4
  • 14