4

I am trying to detect if the browser is Safari. If so, only then do something. In all other browsers, do something else:

if ( navigator.userAgent.toLowerCase().indexOf('safari') == -1) { //if safari execute some function } else { // if other browsers execute other function } 

However, I guess I am not using the right approach because it's not working. :P

3
  • 3
    It should be !=. Never rely on browser detection, but feature detection. Commented Feb 13, 2011 at 21:29
  • There is usually a better way to do this than to sniff the user agent. What is it that you want to do differently in safari? Commented Feb 13, 2011 at 23:55
  • One good reason to sniff the browser is so you can work around a browser-specific bug. For example, I am working around a Safari bug improperly caching resources when Range headers are present in the GET. Commented Jan 20, 2013 at 15:34

3 Answers 3

4
if(typeof navigator.vendor!='undefined') && navigator.vendor.toLowerCase().indexOf('apple')!=-1){ ... } 
Sign up to request clarification or add additional context in comments.

1 Comment

on OSX, Chrome still has "apple" in the userAgent string, so this did not work for me
3

Quirksmode has a Browser Detection Script that you can use to detect the different browsers that are being used and then perform different actions based on that browser type.

Under the hood, it's essentially using the same technique that you are trying to use.

In your example, you actually are close. A quick fix is to just change the == to != and voila, your script should work!

However, I am running Chrome, not Safari! Yet, in my user agent string, I see the following:

"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10" 

The word "Safari" appears in my userAgent String, which means that, using your script, my browser would be treated as if it were Safari!

8 Comments

Both Safari and Chrome interpret JS almost exactly the same, as both are webkit browsers.
@JCOC611 - Very true! I'm not disputing that fact, just the fact that if the OP only wants to run something in Safari and not Chrome, what he's trying to do won't work. I'm not making any assumptions about what he's trying to do.
I completely agree with both of you, just pointing it out. The userAgeint of chrome includes "Safari" so it would be the same as checking for "webkit" technically.
@JCOC611 Sorry if I'm being a little pedantic here, but WebKit is a rendering engine. Chrome and Safari use two different JavaScript engines, so the fact that they both use WebKit doesn't have much to do with the way they run JavaScript.
@Cam - No, believe me, you aren't the only one.
|
1

I ended up using

var isSafari = navigator.userAgent.match(/safari/i) != null && navigator.userAgent.match(/chrome/i) == null; if(isSafari){ // code here } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.