浏览器嗅探

发布于 2024-11-09 22:51:45 字数 970 浏览 2 评论 0原文

我知道浏览器嗅探不是为多个浏览器设计网站的正确方法。然而,我的问题与设计一个对每个浏览器都表现良好的网站无关。

我想为用户提供将网站安装为网络应用程序(如果浏览器是 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 技术交流群。

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

发布评论

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

评论(4

不打扰别人 2024-11-16 22:51:45

看看 jQuery.browser: http://api.jquery.com/jQuery.browser/

$.browser 属性提供
有关网络浏览器的信息
正在访问该页面,如报告所述
浏览器本身。它包含标志
对于四种最普遍的情况中的每一种
浏览器类(Internet Explorer、
Mozilla、Webkit 和 Opera)以及
版本信息。

可用标志有:

webkit(自 jQuery 1.4 起)safari
(已弃用) opera msie mozilla 这
财产立即可用。它
因此可以安全地使用它
决定是否呼叫
$(文档).ready()。 $.浏览器
jQuery 1.3 中不推荐使用该属性,
其功能可能会转移到
未来团队支持的插件
jQuery 发布。

因为 $.browser 使用
navigator.userAgent 来确定
平台容易被欺骗
由用户或不实陈述
浏览器本身。它总是最好的
以避免特定于浏览器的代码
尽可能完全。 $.support
属性可用于检测
支持特定功能而不是
而不是依赖 $.browser。

Take a look at jQuery.browser: http://api.jquery.com/jQuery.browser/

The $.browser property provides
information about the web browser that
is accessing the page, as reported by
the browser itself. It contains flags
for each of the four most prevalent
browser classes (Internet Explorer,
Mozilla, Webkit, and Opera) as well as
version information.

Available flags are:

webkit (as of jQuery 1.4) safari
(deprecated) opera msie mozilla This
property is available immediately. It
is therefore safe to use it to
determine whether or not to call
$(document).ready(). The $.browser
property is deprecated in jQuery 1.3,
and its functionality may be moved to
a team-supported plugin in a future
release of jQuery.

Because $.browser uses
navigator.userAgent to determine the
platform, it is vulnerable to spoofing
by the user or misrepresentation by
the browser itself. It is always best
to avoid browser-specific code
entirely where possible. The $.support
property is available for detection of
support for particular features rather
than relying on $.browser.

动次打次papapa 2024-11-16 22:51:45

不要担心什么被认为是正确的。做有效的事情;在这种情况下,也许浏览器嗅探是最好或唯一的好选择。

Don't worry about what is considered proper. Do what works; in this case perhaps browser sniffing is the best or only good option.

So尛奶瓶 2024-11-16 22:51:45

我从来没有理解仅使用导航器对象中的属性的问题:

<script>
    for(var item in navigator)
    {
        document.write('navigator.' + item + ': ' + navigator[item] + '<br>');
    }
</script>

他们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:

<script>
    for(var item in navigator)
    {
        document.write('navigator.' + item + ': ' + navigator[item] + '<br>');
    }
</script>

They say that the navigator.userAgent is unreliable, but do your research, it can be combined with navigator.appName and navigator.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 use document.uniqueID. Test for document.uniqueID directly.

Everyone knows that using navigator.userAgent to determine if window.localStorage can be used is insane, but they don't realise that ie7: 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.

帅冕 2024-11-16 22:51:45

您可以尝试: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.

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