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;
}
Sarfraz
2010-07-21 21:07:54
uhm -- $.browser.chrome is undefined. and $.browser.safari is true for both chrome and safari
Scott Evernden
2010-07-21 21:19:12
@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
2010-07-21 21:28:27
@sAc, also, the `version` variable is unneeded, and should be replaced with `$.browser.version`.
kingjeffrey
2010-07-22 20:13:37
@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
2010-07-22 20:15:12
@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
2010-07-22 20:46:20
@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
2010-07-23 22:05:37
+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
2010-07-29 16:20:24