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

function IsSafari() {

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

}

当前回答

我知道这个问题很老了,但我还是想把答案贴出来,因为它可能会帮助到别人。上述解决方案在某些极端情况下是失败的,所以我们必须以一种分别处理iOS、Desktop和其他平台的方式来实现它。

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——“版本”。所以这个正则表达式将完美工作:

/.*Version.*Safari.*/.test(navigator.userAgent)

基于@SudarP的回答。

在2021年Q3,这个解决方案将失败在Firefox (Uncaught TypeError: navigator.vendor.match(…)是null)和Chrome (Uncaught TypeError:不能读取null属性(读取'length'));

所以这里有一个固定且简短的解决方案:

function isSafari() {
  return (navigator.vendor.match(/apple/i) || "").length > 0
}

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

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

低温-铬 fxios - Firefox

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

就我而言,我需要同时在iOS和macOS上运行Safari。这招对我很管用:

if (/apple/i.test(navigator.vendor)) {
  // It's Safari
}