javascript - 浏览器嗅探

标签 javascript browser browser-detection

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

如果浏览器是 Google Chrome 或 Firefox 4+,我想为用户提供将网站安装为网络应用程序的能力,如果是 Opera,则作为小部件安装,如果是 Safari,则作为扩展......等等

基本上,我想滑入一个带有提供此类安装的按钮的 div。如果浏览器是例如 Safari,则显示 webapp 解决方案是没有用的,因为 Safari 不支持它。

那么我该如何以好的方式做到这一点呢?

我发现这是基于特性而不是用户代理

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
}

它似乎能满足我的需要,而且很短,也不笨重,或多或少是安全的

最佳答案

看看 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.

关于javascript - 浏览器嗅探,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6134579/

相关文章:

javascript - 为什么浏览器限制并行 HTTP 请求

基于当前浏览器的宽度和高度的 JavaScript CSS 选择

html - 是否可以使用服务器端浏览器检测来确保 IE7 和 IE8 中 html5 语义标签的优雅降级

windows - 从 Linux 告诉 Windows(通过网络)

javascript - JQuery 脚本在 JSFiddle 中有效,但在我自己的浏览器中无效

javascript - 在 JavaFX 中访问 Javascript 的返回值

html - HTML中不关闭标签和属性的实际问题是什么

browser - 带有阻塞 IO 客户端、浏览器的 Netty

Javascript 数组、排序和分支预测

javascript - 为什么浏览器会触发我的 onclick 函数?