gpt4 book ai didi

javascript - 浏览器嗅探

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:02 26 4
gpt4 key购买 nike

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

如果浏览器是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com