Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect chrome and safari browser (webkit)

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!

like image 283
Rouge Avatar asked Sep 27 '12 16:09

Rouge


People also ask

How do I check my browser WebKit?

In order to know if it's webkit in general: isWebkit = /(safari|chrome)/. test(navigator.

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.

What is WebKit in Safari?

Webkit is an open-source web browser engine that was developed by Apple, Inc. It has powered browsers, including Google Chrome, Apple Safari, the default iOS browser, and the default Android browser. For more information about WebKit, visit the WebKit website.


1 Answers

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!"); 
like image 98
Oscar Jara Avatar answered Sep 20 '22 00:09

Oscar Jara