Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  95] [ 1]  / answers: 1 / hits: 129763  / 12 Years ago, thu, september 27, 2012, 12:00:00

I am trying to detect the chrome and safari browser using jquery or javascript.
I thought we are not supposed to use jQuery.browser. Are there any suggestions here? Thanks a lot!


More From » jquery

 Answers
6

If you dont want to use $.browser, take a look at case 1, otherwise maybe case 2 and 3 can help you just to get informed because it is not recommended to use $.browser (the user agent can be spoofed using this). An alternative can be using jQuery.support that will detect feature support and not agent info.



But...



If you insist on getting browser type (just Chrome or Safari) but not using $.browser, case 1 is what you looking for...






This fits your requirement:



Case 1: (No jQuery and no $.browser, just javascript)



Live Demo: http://jsfiddle.net/oscarj24/DJ349/



var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);

if (isChrome) alert(You are using Chrome!);
if (isSafari) alert(You are using Safari!);





These cases I used in times before and worked well but they are not recommended...



Case 2: (Using jQuery and $.browser, this one is tricky)



Live Demo: http://jsfiddle.net/oscarj24/gNENk/



$(document).ready(function(){

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

/* Detect Chrome */
if($.browser.chrome){
/* Do something for Chrome at this point */
/* Finally, if it is Chrome then jQuery thinks it's
Safari so we have to tell it isn't */
$.browser.safari = false;
}

/* Detect Safari */
if($.browser.safari){
/* Do something for Safari */
}

});


Case 3: (Using jQuery and $.browser, elegant solution)



Live Demo: http://jsfiddle.net/oscarj24/uJuEU/



$.browser.chrome = $.browser.webkit && !!window.chrome;
$.browser.safari = $.browser.webkit && !window.chrome;

if ($.browser.chrome) alert(You are using Chrome!);
if ($.browser.safari) alert(You are using Safari!);

[#82871] Wednesday, September 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;