我正在尝试检测浏览器是否为Safari。如果是这样,那就做点什么吧。在所有其他浏览器中,执行其他操作:
if ( navigator.userAgent.toLowerCase().indexOf('safari') == -1) {
//if safari execute some function
} else {
// if other browsers execute other function
}然而,我猜我没有使用正确的方法,因为它不起作用。:P
发布于 2011-02-14 05:31:11
Quirksmode有一个Browser Detection 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"单词"Safari“出现在我的userAgent字符串中,这意味着,使用您的脚本,我的浏览器将被视为Safari!
发布于 2012-06-03 05:34:27
if(typeof navigator.vendor!='undefined') &&
navigator.vendor.toLowerCase().indexOf('apple')!=-1){
...
}发布于 2015-06-10 00:08:51
我最终使用了
var isSafari = navigator.userAgent.match(/safari/i) != null && navigator.userAgent.match(/chrome/i) == null;
if(isSafari){
// code here
}https://stackoverflow.com/questions/4986993
复制相似问题