使用 jQuery.browser 区分 Chrome 和 Safari
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3303858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Distinguish Chrome from Safari using jQuery.browser
提问by kingjeffrey
It seems jQuery.browseris able to identify webkit rather easily as of 1.4. But how can I use it to distinguish Chrome from Safari (and visa-versa)?
从1.4 开始,jQuery.browser似乎能够很容易地识别 webkit。但是我如何使用它来区分 Chrome 和 Safari(反之亦然)?
回答by kingjeffrey
Since Sarfraz has not corrected his answer (thank you Sarfraz for pointing me in the correct direction), I will post functioning code here.
由于 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;
}
回答by Vikrant Chaudhary
WithoutjQuery
没有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);
}
WithjQuery
使用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/)
(以下不适用于 jQuery 1.9 及更高版本,因为jQuery.browser
已从 jQuery 中删除。请参阅http://api.jquery.com/jQuery.browser/)
$.browser.chrome = $.browser.webkit && !!window.chrome;
$.browser.safari = $.browser.webkit && !window.chrome;
回答by Sarfraz
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;
}
回答by Scott Evernden
/Chrome/.test(navigator.userAgent)
回答by Hugo Stieglitz
Also for non-JQuery users:
也适用于非 JQuery 用户:
navigator.userAgent.indexOf('WebKit') + 1 ?
((navigator.vendor || '').indexOf('Apple') + 1 ? /* Safari */ : /* Chrome */)
: /* not Webkit */
回答by Sivalingaamorthy
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;
},
回答by J_z
window.chrome?$.browser.chrome=!0:($.browser.webkit&&($.browser.safari=!0),$.browser.chrome=!1);
This patch adds $.browser.chromeand also exclude Goolge Chrome detection from $.browser.safari
此补丁添加了$.browser.chrome并从$.browser.safari 中排除了 Goolge Chrome 检测