Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish Chrome from Safari using jQuery.browser

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)?

like image 723
kingjeffrey Avatar asked Jul 21 '10 21:07

kingjeffrey


People also ask

How do I identify my browser?

In the browser window, hold the Alt key and press H to bring up the Help menu. Click About Google Chrome and locate the version at the top of the window that appears.

How can you detect the client's browser name in Javascript?

You can use the navigator. appName and navigator. userAgent properties. The userAgent property is more reliable than appName because, for example, Firefox (and some other browsers) may return the string "Netscape" as the value of navigator.


2 Answers

Since Sarfraz has not corrected his answer (thank you Sarfraz 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('version/') +8);   userAgent = userAgent.substring(0,userAgent.indexOf('.'));   $.browser.version = userAgent; } 
like image 74
kingjeffrey Avatar answered Oct 21 '22 03:10

kingjeffrey


Without jQuery

isChrome = function() {    return /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); } isSafari = function() {   return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor); } 

With jQuery

(Following will not work with jQuery 1.9 and above as jQuery.browser has been removed from jQuery. See http://api.jquery.com/jQuery.browser/)

$.browser.chrome = $.browser.webkit && !!window.chrome; $.browser.safari = $.browser.webkit && !window.chrome; 
like image 25
Vikrant Chaudhary Avatar answered Oct 21 '22 02:10

Vikrant Chaudhary