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!
In order to know if it's webkit in general: isWebkit = /(safari|chrome)/. test(navigator.
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.
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.
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...
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!");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With