浏览器嗅探
我知道浏览器嗅探不是为多个浏览器设计网站的正确方法。然而,我的问题与设计一个对每个浏览器都表现良好的网站无关。
我想为用户提供将网站安装为网络应用程序(如果浏览器是 Google Chrome 或 Firefox 4+)的能力,如果浏览器是 Opera,则作为小部件,如果是 Safari,则作为扩展程序...等等
基本上我想滑动在带有提供此类安装按钮的 div 中。如果浏览器是 Safari,则显示 webapp 解决方案是没有用的,因为 Safari 不支持它。
那么我该如何以良好的方式做到这一点呢?
我发现这是基于功能而不是 useragent
安全功能-基于使用 Javascript 检测 Google Chrome 的方法?
var is = {
ff: window.globalStorage,
ie: document.all && !window.opera,
ie6: !window.XMLHttpRequest,
ie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera,
ie8: document.documentMode==8,
opera: Boolean(window.opera),
chrome: Boolean(window.chrome),
safari: window.getComputedStyle && !window.globalStorage && !window.opera
}
它似乎适合我的需求,而且简短且不笨重,或多或少具有欺骗安全性
I know browsersniffing is not the correct way to design a site for multiple browsers. My question however is not related to designing a site which behaves well for each browser.
I want to offer the user the ability to install the site as a webapp if the browser is Google Chrome or Firefox 4+, as a widget if it's Opera, as an extension if it's Safari... and so on
Basically I want to slide in a div with a button offering this kind of install. There is no use showing the webapp solution if the browser is for example Safari as Safari has no support for it.
So how do I do this in a good way?
I found this based on features rather than useragent
Safe feature-based way for detecting Google Chrome with Javascript?
var is = {
ff: window.globalStorage,
ie: document.all && !window.opera,
ie6: !window.XMLHttpRequest,
ie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera,
ie8: document.documentMode==8,
opera: Boolean(window.opera),
chrome: Boolean(window.chrome),
safari: window.getComputedStyle && !window.globalStorage && !window.opera
}
It seems to work for my needs and is short and not bulky and more or less spoof safe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看 jQuery.browser: http://api.jquery.com/jQuery.browser/
Take a look at jQuery.browser: http://api.jquery.com/jQuery.browser/
不要担心什么被认为是正确的。做有效的事情;在这种情况下,也许浏览器嗅探是最好或唯一的好选择。
Don't worry about what is considered proper. Do what works; in this case perhaps browser sniffing is the best or only good option.
我从来没有理解仅使用导航器对象中的属性的问题:
他们说
navigator.userAgent
不可靠,但是请进行研究,它可以与 <我认为 code>navigator.appName 和navigator.vendor
具有很高的可靠性。更新:2013 年 3 月
您必须直接测试您想知道的事情,如果您尝试推断它,那么您就做错了。
例如。如果您想使用某个功能,请直接对其进行测试,不要假设如果
document.all
则可以使用document.uniqueID
。直接测试document.uniqueID
。大家都知道用
navigator.userAgent
来判断是否可以使用window.localStorage
是疯狂的,但是他们没有意识到ie7: document.all & & window.XMLHttpRequest && !XDomainRequest && !window.opera
也在相反的方向做同样的事情。不幸的是,如果您确实想知道用户代理是什么,那么您只能使用
navigator
对象。用户代理字符串欺骗不是问题,无论如何也不是你的问题。
I never understood the problem with just using properties out of the navigator object:
They say that the
navigator.userAgent
is unreliable, but do your research, it can be combined withnavigator.appName
andnavigator.vendor
with high reliability I reckon.UPDATE: March 2013
You have to test directly for the thing that you want to know, if you try to infer it, you're doing it wrong.
For example. If you want to use a feature, test for it directly, don't assume that if
document.all
then you can usedocument.uniqueID
. Test fordocument.uniqueID
directly.Everyone knows that using
navigator.userAgent
to determine ifwindow.localStorage
can be used is insane, but they don't realise thatie7: document.all && window.XMLHttpRequest && !XDomainRequest && !window.opera
is also doing the same thing in the opposite direction.If you actually want to know what the user agent is, then all you can go off is the
navigator
object, unfortunately.User agent string spoofing is not a problem, not yours anyway.
您可以尝试:
BrowserHawk
,(http://www.cyscape.com/showbrow .asp)浏览器在服务器端进行检查。理论上,这将减少浏览器端在确定向最终用户显示什么内容时的处理。但是,我不认为这是免费的。他们的特色客户包括雅虎、AOL、思科、微软和Sun。因此,这仅用于生产用途,并且最终用户访问该站点的期望很高。You can try:
BrowserHawk
, (http://www.cyscape.com/showbrow.asp) Which does the browser checking on the server side. This will theoritically reduce the processing on browser side when determining what to show the end users. However, I dont think this is free. Their featured customers include Yahoo, AOL, Cisco, Microsoft and Sun. So this is for production use only with large expectancies of end users visiting the site.