简体   繁体   English

确定用户是否实际使用Safari的最佳方法是什么?

[英]What is best way to determine if the user is actually using Safari?

确定用户是否实际使用Safari的最佳方法是什么?

You might want to read http://www.quirksmode.org/js/detect.html and then http://www.quirksmode.org/js/support.html and understand that the best you can do is probe what the user tells you is the browser they are using (this would be the User_Agent). 你可能想读http://www.quirksmode.org/js/detect.html然后http://www.quirksmode.org/js/support.html和理解,你能做的最好的是探测用户什么告诉您他们正在使用的浏览器(这将是User_Agent)。 You can never say with 100% certainty that they are using a given browser, because you have to rely on what is transmitted from the browser and it can be overriden at many points. 您永远不能100%地确定他们使用的是给定的浏览器,因为您必须依靠从浏览器传输的内容,并且可以在许多地方覆盖它们。

However, the quirksmode.org solution is usually the best way to deal with a single check across all engines and browsers. 但是,quirksmode.org解决方案通常是处理所有引擎和浏览器中的单个检查的最佳方法。

Are you using a particular javascript framework that could allow you to more accurately determine what they're using? 您是否在使用特定的JavaScript框架,从而可以更准确地确定他们使用的是什么? What serverside language are you using? 您正在使用哪种服务器端语言? (and as somebody else asked, why Safari only?) (正如其他人所问,为什么只使用Safari?)

Others have already noted that browser sniffing is not ideal, but there are cases where a bug only happens in a browser and it can be hard to detect. 其他人已经注意到,浏览器嗅探并不理想,但是在某些情况下,错误仅发生在浏览器中,并且很难检测到。 In that case, you can do browser sniffing and hope the user agent string is valid. 在这种情况下,您可以进行浏览器嗅探,并希望用户代理字符串有效。 Here's what Ext-JS does. 这是Ext-JS的功能。

var ua = navigator.userAgent.toLowerCase();
var isChrome = /\bchrome\b/.test(ua);
var isSafari = !isChrome && /safari/.test(ua);

Line 62 of view-source:http://dev.sencha.com/deploy/dev/docs/source/Ext.html has all of Ext's browser detection in an easy to read format. view-source的第62行:http://dev.sencha.com/deploy/dev/docs/source/Ext.html具有所有Ext浏览器检测的易于阅读的格式。

This is brittle and will probably not work as new versions and browsers come out. 这很脆弱,可能随着新版本和浏览器的出现而无法使用。

Tell us why you're testing for safari, so we can make sure there isn't a better way to achieve what you want. 告诉我们为什么要测试野生动物园,因此我们可以确保没有更好的方法来实现您想要的目标。

if(navigator.userAgent.toLowerCase().indexOf("safari") != -1 && navigator.userAgent.toLowerCase().indexOf("chrome") == -1){
    // you code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 确定新站点用户的最佳方法-Django - Best way to determine a new site user - Django 在 JavaScript 中确定日期是否为今天的最佳方法是什么? - What is the best way to determine if a date is today in JavaScript? 为 Safari 应用程序扩展设置全局设置的最佳方法是什么? - What is the best way to set global settings for Safari app extension? 检测浏览器是否为 safari 及其版本的最佳方法是什么 - What is the best way to detect if the browser is safari and also its version 确定网页是否适用于移动设备的最佳方法是什么? - What is the best way to determine if a web-page is for mobile? 在 JavaScript 中确定 URL 是否属于当前域的最佳方法是什么? - What is the best way to determine if a URL belongs to the current domain in JavaScript? 在网页上可见之前确定DIV高度的最佳方法是什么? - what is the best way to determine a DIV's height before visible on the webpage? 确定给定数字是否为 2 的幂的最佳方法是什么? - What is the best way to determine if a given number is a power of two? 确定Tomcat已从node.js开始的最佳方法是什么 - What is the best way to determine Tomcat has started with node.js 用 JavaScript 确定一个月天数的最佳方法是什么? - What is the best way to determine the number of days in a month with JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM