使用 jQuery.browser 区分 Chrome 和 Safari

发布于 2024-09-11 01:02:21 字数 152 浏览 4 评论 0原文

从 1.4 开始, jQuery.browser 似乎能够很容易地识别 webkit。但我如何使用它来区分 Chrome 和 Safari(反之亦然)?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

瞎闹 2024-09-18 01:02:21

由于 Sarfraz 尚未纠正他的答案(感谢 Sarfraz 为我指明了正确的方向),我将在这里发布功能代码。

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;
}

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;
}
沩ん囻菔务 2024-09-18 01:02:21

不使用 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);
}

使用 jQuery

(以下内容不适用于 jQuery 1.9 及更高版本,因为 jQuery.browser 已从 jQuery 中删除。请参阅 http://api.jquery.com/jQuery.browser/

$.browser.chrome = $.browser.webkit && !!window.chrome;
$.browser.safari = $.browser.webkit && !window.chrome;

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;
萌辣 2024-09-18 01:02:21

您可以这样做:

// 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/

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/

无尽的现实 2024-09-18 01:02:21
/Chrome/.test(navigator.userAgent)
/Chrome/.test(navigator.userAgent)
£噩梦荏苒 2024-09-18 01:02:21

同样适用于非 JQuery 用户:

    navigator.userAgent.indexOf('WebKit') + 1 ? 
       ((navigator.vendor || '').indexOf('Apple') + 1 ? /* Safari */ : /* Chrome */)
    : /* not Webkit */

http://jsfiddle.net/HtWag/13/

Also for non-JQuery users:

    navigator.userAgent.indexOf('WebKit') + 1 ? 
       ((navigator.vendor || '').indexOf('Apple') + 1 ? /* Safari */ : /* Chrome */)
    : /* not Webkit */

http://jsfiddle.net/HtWag/13/

月下伊人醉 2024-09-18 01:02:21

你也可以尝试使用这个方法,它对我有用。

    isSafari: function () 
    {
            var isSafari = (navigator.userAgent.indexOf('Safari') != -1
                        && navigator.userAgent.indexOf('Chrome') == -1)

            console.log('IsSafari : ' + isSafari);

            return isSafari;
    },

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;
    },
沉溺在你眼里的海 2024-09-18 01:02:21
window.chrome?$.browser.chrome=!0:($.browser.webkit&&($.browser.safari=!0),$.browser.chrome=!1);

此补丁添加了 $.browser.chrome 并排除了 $.browser.safari 中的 Google Chrome 检测

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文