views:

204

answers:

3

It seems jQuery.browser is able to identify webkit rather easily as of 1.4. But how can I use it to distinguish Chrome from Safari (and visa-versa)?

+1  A: 

You can do like:

// Is this a version of Chrome?
if($.browser.chrome){
  userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
  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('.'));
  version = userAgent;
}

http://api.jquery.com/jQuery.browser/

Sarfraz
uhm -- $.browser.chrome is undefined. and $.browser.safari is true for both chrome and safari
Scott Evernden
@sAc, you were close, this should appear before the code you provided: `var userAgent = navigator.userAgent.toLowerCase(); $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); var version = 0;`
kingjeffrey
@sAc, also, the `version` variable is unneeded, and should be replaced with `$.browser.version`.
kingjeffrey
@sAc, I'd like to accept your answer, as it put me on the right track – but am unable to do so as long as it contains non-operable code. Please edit your answer to correct the errors mentioned above.
kingjeffrey
@kingjeffrey: I posted the code from jquery docs for which link is posted in my answer, it was not written by me. And any fixes are already mentioned in these comments :)
Sarfraz
@sAc, you pasted a portion of the code that lost significant context. While it pointed me in the right direction, this loss of context cripples the provided code. I'd edit it myself, except I do not allowed by my reputation.
kingjeffrey
A: 
/Chrome/.test(navigator.userAgent)
Scott Evernden
+1  A: 

Since sAc has not corrected his answer (thank you sAc for pointing me in the correct direction), I will post functioning code here.

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