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?

2

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

2 Comments

ah great this will probably do the trick. Can you confirm if I can pass in 'msie', 'opera' and 'mozilla' into these if else statements too?
@Cliftwalker: yes. you can. In lowercase!
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/

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.