Javascript 使用 jQuery 检测 Safari
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5899783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Safari using jQuery
提问by AndreKR
Though both are Webkit based browsers, Safari urlencodes quotation marks in the URL while Chrome does not.
尽管两者都是基于 Webkit 的浏览器,但 Safari 会对 URL 中的引号进行 urlencode,而 Chrome 则不会。
Therefore I need to distinguish between these two in JS.
因此我需要在 JS 中区分这两者。
jQuery's browser detection docsmark "safari" as deprecated.
jQuery 的浏览器检测文档将“safari”标记为已弃用。
Is there a better method or do I just stick with the deprecated value for now?
有没有更好的方法,还是我现在就坚持使用已弃用的值?
回答by Panos Kal.
Using a mix of feature detection
and Useragent
string:
使用feature detection
和Useragent
字符串的混合:
var is_opera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var is_Edge = navigator.userAgent.indexOf("Edge") > -1;
var is_chrome = !!window.chrome && !is_opera && !is_Edge;
var is_explorer= typeof document !== 'undefined' && !!document.documentMode && !is_Edge;
var is_firefox = typeof window.InstallTrigger !== 'undefined';
var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
Usage:if (is_safari) alert('Safari');
用法:if (is_safari) alert('Safari');
Or for Safari only, use this :
或者仅适用于 Safari,请使用以下命令:
if ( /^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {alert('Its Safari');}
回答by brissmyr
The following identifies Safari 3.0+ and distinguishes it from Chrome:
以下标识 Safari 3.0+ 并将其与 Chrome 区分开来:
isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)
回答by user3107045
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
不幸的是,上面的例子也会将 android 的默认浏览器检测为 Safari,但事实并非如此。我用了
navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1 && navigator.userAgent.indexOf('Android') == -1
回答by FlamyTwista
For checking Safari I used this:
为了检查 Safari,我使用了这个:
$.browser.safari = ($.browser.webkit && !(/chrome/.test(navigator.userAgent.toLowerCase())));
if ($.browser.safari) {
alert('this is safari');
}
It works correctly.
它工作正常。
回答by AndreKR
Apparently the only reliable and accepted solution would be to do feature detection like this:
显然,唯一可靠且可接受的解决方案是像这样进行特征检测:
browser_treats_urls_like_safari_does = false;
var last_location_hash = location.hash;
location.hash = '"blah"';
if (location.hash == '#%22blah%22')
browser_treats_urls_like_safari_does = true;
location.hash = last_location_hash;
回答by ohrlando
The only way I found is check if navigator.userAgent contains iPhone or iPad word
我发现的唯一方法是检查 navigator.userAgent 是否包含 iPhone 或 iPad 词
if (navigator.userAgent.toLowerCase().match(/(ipad|iphone)/)) {
//is safari
}
回答by Code Spy
//Check if Safari
function isSafari() {
return /^((?!chrome).)*safari/i.test(navigator.userAgent);
}
//Check if MAC
if(navigator.userAgent.indexOf('Mac')>1){
alert(isSafari());
}
回答by John Strickler
If you are checking the browser use $.browser
. But if you are checking feature support (recommended) then use $.support
.
如果您正在检查浏览器,请使用$.browser
. 但是,如果您正在检查功能支持(推荐),请使用$.support
.
You should NOT use $.browser to enable/disable features on the page. Reason being its not dependable and generally just not recommended.
你不应该使用 $.browser 来启用/禁用页面上的功能。原因是它不可靠,一般不推荐。
If you need feature support then I recommend modernizr.
如果您需要功能支持,那么我推荐Modernizr。
回答by user1330204
Generic FUNCTION
通用功能
var getBrowseActive = function (browserName) {
return navigator.userAgent.indexOf(browserName) > -1;
};
回答by reicek
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.
解决这个问题的一个非常有用的方法是检测浏览器的 webkit 版本并检查它是否至少是我们需要的版本,否则做其他事情。
Using jQuery it goes like this:
使用 jQuery 它是这样的:
"use strict";
$(document).ready(function() {
var appVersion = navigator.appVersion;
var webkitVersion_positionStart = appVersion.indexOf("AppleWebKit/") + 12;
var webkitVersion_positionEnd = webkitVersion_positionStart + 3;
var webkitVersion = appVersion.slice(webkitVersion_positionStart, webkitVersion_positionEnd);
console.log(webkitVersion);
if (webkitVersion < 537) {
console.log("webkit outdated.");
} else {
console.log("webkit ok.");
};
});
This provides a safe and permanent fix for dealing with problems with browser's different webkit implementations.
这为处理浏览器不同 webkit 实现的问题提供了安全和永久的修复。
Happy coding!
快乐编码!