如何检测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浏览器。

其他回答

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

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

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

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

为上面的答案修改正则表达式

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

低温-铬 fxios - Firefox

也许这样行得通:

Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor')

编辑:不再工作

只有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>