ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 浏览器嗅探

2019-06-26 16:31:02  阅读:308  来源: 互联网

标签:javascript browser browser-detection


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

我想让用户能够将网站安装为webapp,如果浏览器是谷歌浏览器或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,browser,browser-detection
来源: https://codeday.me/bug/20190626/1295857.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有