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

function IsSafari() {

  var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
  return is_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>

其他回答

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

你可以很容易地使用索引的Chrome过滤Chrome:

var ua = navigator.userAgent.toLowerCase(); 
if (ua.indexOf('safari') != -1) { 
  if (ua.indexOf('chrome') > -1) {
    alert("1") // Chrome
  } else {
    alert("2") // Safari
  }
}

用户代理嗅探非常棘手且不可靠。我们尝试着用@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上测试。

我创建了一个返回布尔类型的函数:

export const isSafari = () => navigator.userAgent.toLowerCase().indexOf('safari') !== -1

此代码仅用于检测safari浏览器

if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) 
{
   alert("Browser is Safari");          
}