使用 jQuery 检测 Safari
虽然两者都是基于 Webkit 的浏览器,但 Safari 会对 URL 中的引号进行 urlencode,而 Chrome 则不会。
因此我需要在JS中区分这两者。
jQuery 的浏览器检测文档 将“safari”标记为已弃用。
有更好的方法还是我暂时坚持使用已弃用的值?
Though both are Webkit based browsers, Safari urlencodes quotation marks in the URL while Chrome does not.
Therefore I need to distinguish between these two in JS.
jQuery's browser detection docs mark "safari" as deprecated.
Is there a better method or do I just stick with the deprecated value for now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
混合使用
功能检测
和Useragent
字符串:用法:
if (is_safari)alert('Safari');
或者仅适用于 Safari,请使用以下命令:
Using a mix of
feature detection
andUseragent
string:Usage:
if (is_safari) alert('Safari');
Or for Safari only, use this :
以下标识了 Safari 3.0+ 并与 Chrome 区分开来:
The following identifies Safari 3.0+ and distinguishes it from Chrome:
不幸的是,上面的例子也会将 Android 的默认浏览器检测为 Safari,但事实并非如此。
我用过
navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1 && navigator.userAgent.indexOf('Android') == -1
unfortunately the above examples will also detect android's default browser as Safari, which it is not.
I used
navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1 && navigator.userAgent.indexOf('Android') == -1
为了检查 Safari,我使用了这个:
它工作正常。
For checking Safari I used this:
It works correctly.
显然,唯一可靠且可接受的解决方案是进行如下特征检测:
Apparently the only reliable and accepted solution would be to do feature detection like this:
我发现的唯一方法是检查 navigator.userAgent 是否包含 iPhone 或 iPad 单词
The only way I found is check if navigator.userAgent contains iPhone or iPad word
如果您要检查浏览器,请使用
$.browser
。但如果您要检查功能支持(推荐),请使用$.support
。您不应该使用 $.browser 来启用/禁用页面上的功能。原因是它不可靠并且通常不推荐。
如果您需要功能支持,那么我推荐 modernizr。
If you are checking the browser use
$.browser
. But if you are checking feature support (recommended) then use$.support
.You should NOT use $.browser to enable/disable features on the page. Reason being its not dependable and generally just not recommended.
If you need feature support then I recommend modernizr.
http://jsfiddle.net/s1o943gb/10/
http://jsfiddle.net/s1o943gb/10/
这将确定浏览器是否为 Safari。
This will determine whether the browser is Safari or not.
我用这个简单的 JavaScript 条件来检测 Apple 浏览器引擎:
I use to detect Apple browser engine, this simple JavaScript condition:
解决此问题的一种非常有用的方法是检测浏览器的 webkit 版本并检查它是否至少是我们需要的版本,否则执行其他操作。
使用 jQuery 它是这样的:
这为处理浏览器的不同 webkit 实现的问题提供了安全且永久的修复。
快乐编码!
A very useful way to fix this is to detect the browsers webkit version and check if it is at least the one we need, else do something else.
Using jQuery it goes like this:
This provides a safe and permanent fix for dealing with problems with browser's different webkit implementations.
Happy coding!
通用功能
Generic FUNCTION
我的最佳解决方案
My best solution