Distinguish Chrome from Safari using jQuery.browser

后端 未结 7 902
谎友^
谎友^ 2020-11-27 16:30

It seems jQuery.browser is able to identify webkit rather easily as of 1.4. But how can I use it to distinguish Chrome from Safari (and visa-versa)?

相关标签:
7条回答
  • 2020-11-27 16:37

    Since Sarfraz has not corrected his answer (thank you Sarfraz for pointing me in the correct direction), I will post functioning code here.

    var userAgent = navigator.userAgent.toLowerCase(); 
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    // Is this a version of Chrome?
    if($.browser.chrome){
      userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
      userAgent = userAgent.substring(0,userAgent.indexOf('.'));
      $.browser.version = userAgent;
      // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
      $.browser.safari = false;
    }
    
    // Is this a version of Safari?
    if($.browser.safari){
      userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
      userAgent = userAgent.substring(0,userAgent.indexOf('.'));
      $.browser.version = userAgent;
    }
    
    0 讨论(0)
  • 2020-11-27 16:41

    You can try using this approach as well, it works for me.

        isSafari: function () 
        {
                var isSafari = (navigator.userAgent.indexOf('Safari') != -1
                            && navigator.userAgent.indexOf('Chrome') == -1)
    
                console.log('IsSafari : ' + isSafari);
    
                return isSafari;
        },
    
    0 讨论(0)
  • 2020-11-27 16:50
    window.chrome?$.browser.chrome=!0:($.browser.webkit&&($.browser.safari=!0),$.browser.chrome=!1);
    

    This patch adds $.browser.chrome and also exclude Goolge Chrome detection from $.browser.safari

    0 讨论(0)
  • 2020-11-27 16:51

    Without jQuery

    isChrome = function() { 
      return /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
    }
    isSafari = function() {
      return /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
    }
    

    With jQuery

    (Following will not work with jQuery 1.9 and above as jQuery.browser has been removed from jQuery. See http://api.jquery.com/jQuery.browser/)

    $.browser.chrome = $.browser.webkit && !!window.chrome;
    $.browser.safari = $.browser.webkit && !window.chrome;
    
    0 讨论(0)
  • 2020-11-27 16:51
    /Chrome/.test(navigator.userAgent)
    
    0 讨论(0)
  • 2020-11-27 16:56

    You can do like:

    // Is this a version of Chrome?
    if($.browser.chrome){
      userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
      userAgent = userAgent.substring(0,userAgent.indexOf('.'));
      version = userAgent;
      // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
      $.browser.safari = false;
    }
    
    // Is this a version of Safari?
    
    if($.browser.safari){
      userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
      userAgent = userAgent.substring(0,userAgent.indexOf('.'));
      version = userAgent;
    }
    

    http://api.jquery.com/jQuery.browser/

    0 讨论(0)
提交回复
热议问题