如何检测Safari浏览器使用JavaScript?我尝试了下面的代码,它不仅检测Safari浏览器,而且还检测Chrome浏览器。
function IsSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
如何检测Safari浏览器使用JavaScript?我尝试了下面的代码,它不仅检测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>
其他回答
此代码仅用于检测safari浏览器
if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0)
{
alert("Browser is Safari");
}
注意:总是尝试检测您试图修复的特定行为,而不是针对它与isSafari?
作为最后的手段,用下面的正则表达式检测Safari:
var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
它使用负环视,排除Chrome、Edge和所有在用户代理中包含Safari名称的Android浏览器。
我测试了#Christopher Martin发布的代码,它把我的浏览器报告为Chrome,因为它在测试Edge之前先测试了这一点,否则Edge会符合旨在识别Chrome的测试。我修改了他的回答,以纠正这一缺陷和其他两个缺陷,即:
Edge的缩写用户代理子字符串 MSIE的旧字符串
将代码转换为函数会产生以下通过调试控制台报告的函数和测试脚本。
<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>
检测手势变化,像这样:
const isSafari = 窗口类型。手势事件 === “函数” document.getElementById(“result”).textContent = isSafari ?“野生动物园!” :“不是野生动物园!” <div id=“result” style=“font-family: sans-serif”></div>
手势改变只在Safari上得到了广泛的支持(iOS上也可以)!
只有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>