Detect Safari using jQuery

前端 未结 13 1572
面向向阳花
面向向阳花 2020-11-27 10:09

Though both are Webkit based browsers, Safari urlencodes quotation marks in the URL while Chrome does not.

Therefore I need to distinguish between these two in JS.

相关标签:
13条回答
  • 2020-11-27 10:18

    The only way I found is check if navigator.userAgent contains iPhone or iPad word

    if (navigator.userAgent.toLowerCase().match(/(ipad|iphone)/)) {
        //is safari
    }
    
    0 讨论(0)
  • 2020-11-27 10:21

    For checking Safari I used this:

    $.browser.safari = ($.browser.webkit && !(/chrome/.test(navigator.userAgent.toLowerCase())));
    if ($.browser.safari) {
        alert('this is safari');
    }
    

    It works correctly.

    0 讨论(0)
  • 2020-11-27 10:21

    A very useful way to fix this is to detect the browsers webkit version and check if it is at least the one we need, else do something else.

    Using jQuery it goes like this:

    "use strict";
    
    $(document).ready(function() {
        var appVersion                  = navigator.appVersion;
        var webkitVersion_positionStart = appVersion.indexOf("AppleWebKit/") + 12;
        var webkitVersion_positionEnd   = webkitVersion_positionStart + 3;
        var webkitVersion               = appVersion.slice(webkitVersion_positionStart, webkitVersion_positionEnd);
    	
        console.log(webkitVersion);
    
        if (webkitVersion < 537) {
            console.log("webkit outdated.");
        } else {
            console.log("webkit ok.");
        };
    });

    This provides a safe and permanent fix for dealing with problems with browser's different webkit implementations.

    Happy coding!

    0 讨论(0)
  • 2020-11-27 10:22

    Apparently the only reliable and accepted solution would be to do feature detection like this:

    browser_treats_urls_like_safari_does = false;
    var last_location_hash = location.hash;
    location.hash = '"blah"';
    if (location.hash == '#%22blah%22')
        browser_treats_urls_like_safari_does = true;
    location.hash = last_location_hash;
    
    0 讨论(0)
  • 2020-11-27 10:23

    This will determine whether the browser is Safari or not.

    if(navigator.userAgent.indexOf('Safari') !=-1 && navigator.userAgent.indexOf('Chrome') == -1)
    {
        alert(its safari);
    }else {
        alert('its not safari');
    }
    
    0 讨论(0)
  • 2020-11-27 10:25

    The following identifies Safari 3.0+ and distinguishes it from Chrome:

    isSafari = !!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)
    
    0 讨论(0)
提交回复
热议问题