如何检测Safari浏览器使用JavaScript?我尝试了下面的代码,它不仅检测Safari浏览器,而且还检测Chrome浏览器。

function IsSafari() {

  var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
  return is_safari;

}

当前回答

注意:总是尝试检测您试图修复的特定行为,而不是针对它与isSafari?

作为最后的手段,用下面的正则表达式检测Safari:

var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);

它使用负环视,排除Chrome、Edge和所有在用户代理中包含Safari名称的Android浏览器。

其他回答

用户代理嗅探非常棘手且不可靠。我们尝试着用@qingu上面的回答去检测iOS上的Safari,它在Safari, Chrome和Firefox上都运行得很好。但它错误地将Opera和Edge检测为Safari。

所以我们使用了功能检测,就像今天一样,serviceWorker只在Safari中被支持,而在iOS上的任何其他浏览器中都不支持。如https://jakearchibald.github.io/isserviceworkerready/所述

支持不包括该平台上第三方浏览器的iOS版本(参见Safari支持)。

所以我们做了一些

if ('serviceWorker' in navigator) {
    return 'Safari';
}
else {
    return 'Other Browser';
}

注意:未在MacOS的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>

我测试了#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>

这个独特的“问题”是100%的标志,浏览器是Safari(信不信由你)。

if (Object.getOwnPropertyDescriptor(Document.prototype, 'cookie').descriptor === false) {
   console.log('Hello Safari!');
}

这意味着cookie对象描述符在Safari上设置为假,而在其他所有项目上设置为真,这实际上让我在其他项目上头疼。编码快乐!

为了记录,我发现最安全的方法是实现下面答案中的浏览器检测代码的Safari部分:

const isSafari = window['safari'] && safari.pushNotification &&
    safari.pushNotification.toString() === '[object SafariRemoteNotification]';

当然,如果可能的话,处理特定于浏览器的问题的最佳方法总是进行特性检测。不过,使用上面这样的一段代码仍然比代理字符串检测要好。