如何使用JavaScript检测Safari浏览器?我尝试了下面的代码,但它不仅检测到了Safari浏览器,还检测到了Chrome浏览器。
function IsSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
如何使用JavaScript检测Safari浏览器?我尝试了下面的代码,但它不仅检测到了Safari浏览器,还检测到了Chrome浏览器。
function IsSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
仅适用于 Safari,不包括 Chrome:
在试用其他代码后,我没有找到任何适用于新旧版本 Safari 的代码。
最终,我编写了此代码,对我非常有效:
var ua = navigator.userAgent.toLowerCase();
var isSafari = false;
try {
isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
}
catch(err) {}
isSafari = (isSafari || ((ua.indexOf('safari') != -1)&& (!(ua.indexOf('chrome')!= -1) && (ua.indexOf('version/')!= -1))));
//test
if (isSafari)
{
//Code for Safari Browser (Desktop and Mobile)
document.getElementById('idbody').innerHTML = "This is Safari!";
}
else
{
document.getElementById('idbody').innerHTML = "Not is Safari!";
}
<body id="idbody">
</body>
/.*Version.*Safari.*/.test(navigator.userAgent)
我使用这个
function getBrowserName() {
var name = "Unknown";
if(navigator.userAgent.indexOf("MSIE")!=-1){
name = "MSIE";
}
else if(navigator.userAgent.indexOf("Firefox")!=-1){
name = "Firefox";
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
name = "Opera";
}
else if(navigator.userAgent.indexOf("Chrome") != -1){
name = "Chrome";
}
else if(navigator.userAgent.indexOf("Safari")!=-1){
name = "Safari";
}
return name;
}
if( getBrowserName() == "Safari" ){
alert("You are using Safari");
}else{
alert("You are surfing on " + getBrowserName(name));
}
function isSafari() {
if (navigator.vendor.match(/[Aa]+pple/g).length > 0 )
return true;
return false;
}
navigator.vendor.toLowerCase().indexOf('apple') > -1
。其中,navigator.vendor
表示浏览器供应商的名称,.toLowerCase()
将其转换为小写字母,.indexOf('apple')
表示查找字符串中是否包含"apple",如果包含,则返回该子字符串的第一个字符在原始字符串中的索引值。最后,判断索引是否大于-1,若是则表示包含"apple",否则不包含。 - fviktorif (navigator.vendor.match(/apple/i)) { ... }
,意思是如果浏览器的供应商包含"apple"(不区分大小写),则执行括号内的代码。 - Bradnavigator.vendor.indexOf("pple")
,但实际上并没有速度差异。 - Infigon上面答案的正则表达式已进行修改(详见此处)
var isSafari = /^((?!chrome|android|crios|fxios).)*safari/i.test(navigator.userAgent);
我知道这个问题很老了,但我还是想发布答案,因为它可能会对某些人有所帮助。以上解决方案在某些边缘情况下失败了,因此我们不得不以一种分别处理iOS、桌面和其他平台的方式来实现它。
function isSafari() {
var ua = window.navigator.userAgent;
var iOS = !!ua.match(/iP(ad|od|hone)/i);
var hasSafariInUa = !!ua.match(/Safari/i);
var noOtherBrowsersInUa = !ua.match(/Chrome|CriOS|OPiOS|mercury|FxiOS|Firefox/i)
var result = false;
if(iOS) { //detecting Safari in IOS mobile browsers
var webkit = !!ua.match(/WebKit/i);
result = webkit && hasSafariInUa && noOtherBrowsersInUa
} else if(window.safari !== undefined){ //detecting Safari in Desktop Browsers
result = true;
} else { // detecting Safari in other platforms
result = hasSafariInUa && noOtherBrowsersInUa
}
return result;
}
记录一下,我发现最安全的方法是实现来自这个回答的浏览器检测代码中的Safari部分:
Original Answer翻译成"最初的回答"
const isSafari = window['safari'] && safari.pushNotification &&
safari.pushNotification.toString() === '[object SafariRemoteNotification]';
当然,处理特定于浏览器的问题的最佳方式始终是进行功能检测(如果可能的话)。虽然使用上述代码片段仍优于代理字符串检测。
原始答案:最初的回答
if (Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').descriptor === false) {
console.log('Hello Safari!');
}
基于@SudarP的答案。
在2021年第三季度,这个解决方案将在Firefox (Uncaught TypeError: navigator.vendor.match(...) is null
)和Chrome (Uncaught TypeError: Cannot read properties of null (reading 'length')
)中失败;
因此,这里有一个修复并更短的解决方案:
function isSafari() {
return (navigator.vendor.match(/apple/i) || "").length > 0
}
我测试了#Christopher Martin发布的代码,它报告我的浏览器为Chrome,因为它在测试Edge之前先测试Chrome,否则会对旨在识别Chrome的测试回答true。 我修改了他的答案以纠正这个缺陷和另外两个缺陷,即:
将代码转换为函数得到以下函数和测试脚本,通过调试控制台报告。
<script type="text/javascript">
function BasicBrowserSniffer ( )
{
if ( navigator.userAgent.match ( /edge\//i ) ) {
return 'edge/edgehtml';
}
if ( navigator.userAgent.match ( /edg\//i ) ) {
return 'edge/edgehtml';
}
else if ( navigator.vendor.match ( /google/i ) ) {
return 'chrome/blink';
}
else if ( navigator.vendor.match ( /apple/i ) ) {
return 'safari/webkit';
}
else if ( navigator.userAgent.match ( /firefox\//i ) ) {
return 'firefox/gecko';
}
else if ( navigator.userAgent.match ( /trident\//i ) ) {
return 'ie/trident';
}
else if ( navigator.userAgent.match ( /msie\//i ) ) {
return 'ie/trident';
}
else
{
return navigator.userAgent + "\n" + navigator.vendor;
}
}; // BasicBrowserSniffer function
console.info ( 'Entering function anonymous DocumentReady function' );
console.info ( 'User Agent String = ' + navigator.userAgent.toLowerCase ( ));
console.info ( 'User Agent Vendor = ' + var uav = navigator.vendor.toLowerCase ( );
console.info ( 'BasicBrowserSniffer = ' + BasicBrowserSniffer ( ) );
console.info ( 'Leaving function anonymous DocumentReady function' );
</script>