Navigator.useragent
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')
Solution 1:
if(typeof navigator.vendor!='undefined') &&
navigator.vendor.toLowerCase().indexOf('apple')!=-1){
...
}
Solution 2:
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!
Solution 3:
I ended up using
var isSafari = navigator.userAgent.match(/safari/i) != null && navigator.userAgent.match(/chrome/i) == null;
if(isSafari){
// code here
}
Post a Comment for "Navigator.useragent"