Navigator.userAgent

I am trying to determine if the browser is Safari. If so, just 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 think I am not using the correct approach because it does not work .: P

+3
source share
3 answers

Quirksmode has a “Browser Detection” Script that you can use to detect the various browsers that are used, and then perform various actions based on this type of browser.

Under the hood, essentially the same method you are trying to use is used.

. - == != voila, script !

Chrome, Safari! , :

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

UserAgent String "Safari", , script , Safari!

+3
if(typeof navigator.vendor!='undefined') &&
   navigator.vendor.toLowerCase().indexOf('apple')!=-1){
    ...
}
+4

I ended up using

var isSafari = navigator.userAgent.match(/safari/i) != null && navigator.userAgent.match(/chrome/i) == null;

if(isSafari){
    // code here
  }
0
source

All Articles