Take the 2-minute tour ×

Though both are Webkit based browsers, Safari urlencodes quotation marks in the URL while Chrome does not.

Therefore I need to distinguish between these two in JS.

jQuery's browser detection docs mark "safari" as deprecated.

Is there a better method or do I just stick with the deprecated value for now?

share|improve this question
I don't know if sticking with it is such a good idea, I haven't checked this out in depth, although I just browsed to the $.browser docs on chrome and it flags $.browser.safari === true. eeek. –  davin May 5 '11 at 15:03
possible duplicate of How to detect Safari, Chrome, IE, Firefox and Opera browser without user agent sniffing?. –  Rob W May 25 '12 at 10:44
add comment

6 Answers

up vote 86 down vote accepted

Chrome has both 'Chrome' and 'Safari' inside userAgent string.
Safari has only 'Safari'.

So this works:

var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_explorer = navigator.userAgent.indexOf('MSIE') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_safari = navigator.userAgent.indexOf("Safari") > -1;
var is_Opera = navigator.userAgent.indexOf("Presto") > -1;
if ((is_chrome)&&(is_safari)) {is_safari=false;}

if (is_safari) alert('Safari');

Or for Safari only, use this :

if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) {alert('Its Safari');}
share|improve this answer
With a ratio of 48 votes vs. 5 votes for a feature detection with side effects, apparently this is the recommended solution. :) Making it accepted answer. –  AndreKR Sep 5 '13 at 16:08
As an example of just how fragile this sort of thing is, this code does not detect Internet Explorer 11 because the UA string has changed. See msdn.microsoft.com/en-us/library/ie/hh869301%28v=vs.85%29.aspx –  Olly Hodgson Mar 19 at 10:16
Android webview will also say Safari and that's not correct –  curtis Jun 16 at 23:39
add comment

For checking Safari I used this:

$.browser.safari = ($.browser.webkit && !(/chrome/.test(navigator.userAgent.toLowerCase())));
if ($.browser.safari) {
    alert('this is safari');

It works correctly.

share|improve this answer
@tranzitoria - thanks it works for me +1 ...... –  ELAYARAJA Aug 31 '13 at 11:03
was removed with jquery 1.9 api.jquery.com/jQuery.browser –  crudolf Nov 23 '13 at 12:34
add comment

The following identifies Safari 3.0+ and distinguishes it from Chrome:

isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)
share|improve this answer
This one worked for me when none of the others, thanks. –  andygoestohollywood Dec 5 '13 at 10:55
add comment

Apparently the only reliable and accepted solution would be to do feature detection like this:

browser_treats_urls_like_safari_does = false;
var last_location_hash = location.hash;
location.hash = '"blah"';
if (location.hash == '#%22blah%22')
    browser_treats_urls_like_safari_does = true;
location.hash = last_location_hash;
share|improve this answer
Nice trick, but unfortunately it doesn't work. The equality (location.hash == '#%22blah%22') will not work because of the way location.hash treats the string. :/ –  Kabamaru Oct 14 '11 at 12:45
Feature detection is the way to go, but there's a better method which does not have any side effects. Your snippet may interfere with other scripts which rely on hash change events. I've marked the question as a duplicate of this. I've created and tested the method in Safari 3.0 - 5.1.3 (Mac and Windows). It's a one-liner :) –  Rob W May 25 '12 at 10:42
add comment

If you are checking the browser use $.browser. But if you are checking feature support (recommended) then use $.support.

You should NOT use $.browser to enable/disable features on the page. Reason being its not dependable and generally just not recommended.

If you need feature support then I recommend modernizr.

share|improve this answer
All true, although not particularly relevant... –  davin May 5 '11 at 15:05
Teach a man to fish... "Is there a better method" - Yes, feature detection. –  John Strickler May 5 '11 at 22:03
You mean something like location.hash = '"blah"'; if (location.hash == '#%22blah%22') alert('is Safari');? –  AndreKR May 6 '11 at 12:26
@Greg You aren't asking the browser.. you are testing the browser. –  John Strickler Jun 16 '11 at 13:10
Feature detection is great, but what about when you want to stop a particular browser from using CSS Animations (for example), because it has a buggy implementation? It technically supports the feature, but we want to take a decision to disable it for that browser, because the experience is better without in that instance. –  Replete Jun 30 '11 at 15:09
show 5 more comments

unfortunately the above examples will also detect android's default browser as Safari, which it is not. I used navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1 && navigator.userAgent.indexOf('Android') == -1

share|improve this answer
add comment

Your Answer


