How can you detect the version of a browser?

后端 未结 28 1906
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:35

I\'ve been searching around for code that would let me detect if the user visiting the website has Firefox 3 or 4. All I have found is code to detect the type of browser but

相关标签:
28条回答
  • 2020-11-22 00:10

    I wrote this for my needs.

    It get info like if is a mobile device or if has a retina display

    try it

    var nav = {
            isMobile:function(){
                return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) != null);
            },
            isDesktop:function(){
                return (navigator.userAgent.match(/iPhone|iPad|iPod|Android|BlackBerry|Opera Mini|IEMobile/i) == null);
            },
            isAndroid: function() {
                return navigator.userAgent.match(/Android/i);
            },
            isBlackBerry: function() {
                return navigator.userAgent.match(/BlackBerry/i);
            },
            isIOS: function() {
                return navigator.userAgent.match(/iPhone|iPad|iPod/i);
            },
            isOpera: function() {
                return navigator.userAgent.match(/Opera Mini/i);
            },
            isWindows: function() {
                return navigator.userAgent.match(/IEMobile/i);
            },
            isRetina:function(){
                return window.devicePixelRatio && window.devicePixelRatio > 1;
            },
            isIPad:function(){
                isIPad = (/ipad/gi).test(navigator.platform);
                return isIPad;
            },
            isLandscape:function(){
                if(window.innerHeight < window.innerWidth){
                    return true;
                }
                return false;
            },
            getIOSVersion:function(){
                if(this.isIOS()){
                    var OSVersion = navigator.appVersion.match(/OS (\d+_\d+)/i);
                    OSVersion = OSVersion[1] ? +OSVersion[1].replace('_', '.') : 0;
                    return OSVersion;
                }
                else
                    return false;
            },
            isStandAlone:function(){
                if(_.is(navigator.standalone))
                    return navigator.standalone;
                return false;
            },
            isChrome:function(){
                var isChrome = (/Chrome/gi).test(navigator.appVersion);
                var isSafari = (/Safari/gi).test(navigator.appVersion)
                return isChrome && isSafari;
            },
            isSafari:function(){
                var isSafari = (/Safari/gi).test(navigator.appVersion)
                var isChrome = (/Chrome/gi).test(navigator.appVersion)
                return !isChrome && isSafari;
            }
    }
    
    0 讨论(0)
  • 2020-11-22 00:12

    You can see what the browser says, and use that information for logging or testing multiple browsers.

    navigator.sayswho= (function(){
        var ua= navigator.userAgent, tem, 
        M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
        if(/trident/i.test(M[1])){
            tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
            return 'IE '+(tem[1] || '');
        }
        if(M[1]=== 'Chrome'){
            tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
            if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
        }
        M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
        if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
        return M.join(' ');
    })();
    
    console.log(navigator.sayswho); // outputs: `Chrome 62`

    0 讨论(0)
  • 2020-11-22 00:12

    jQuery can handle this quite nice (jQuery.browser)

    var ua = $.browser;
    if ( ua.mozilla && ua.version.slice(0,3) == "1.9" ) {
        alert( "Do stuff for firefox 3" );
    }
    

    EDIT: As Joshua wrote in his comment below, jQuery.browser property is no longer supported in jQuery since version 1.9 (read jQuery 1.9 release notes for more details). jQuery development team recommends using more complete approach like adapting UI with Modernizr library.

    0 讨论(0)
  • 2020-11-22 00:13

    This combines kennebec's (K) answer with Hermann Ingjaldsson's (H) answer:

    • Maintains original answer's minimal code. (K)
    • Works with Microsoft Edge (K)
    • Extends the navigator object instead of creating a new variable/object. (K)
    • Separates browser version and name into independent child objects. (H)

     

    navigator.browserSpecs = (function(){
        var ua = navigator.userAgent, tem, 
            M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
        if(/trident/i.test(M[1])){
            tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
            return {name:'IE',version:(tem[1] || '')};
        }
        if(M[1]=== 'Chrome'){
            tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
            if(tem != null) return {name:tem[1].replace('OPR', 'Opera'),version:tem[2]};
        }
        M = M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
        if((tem = ua.match(/version\/(\d+)/i))!= null)
            M.splice(1, 1, tem[1]);
        return {name:M[0], version:M[1]};
    })();
    
    console.log(navigator.browserSpecs); //Object { name: "Firefox", version: "42" }
    
    if (navigator.browserSpecs.name == 'Firefox') {
        // Do something for Firefox.
        if (navigator.browserSpecs.version > 42) {
            // Do something for Firefox versions greater than 42.
        }
    }
    else {
        // Do something for all other browsers.
    }
    
    0 讨论(0)
  • 2020-11-22 00:13
    var ua = navigator.userAgent;
    
    if (/Firefox\//.test(ua))
       var Firefox = /Firefox\/([0-9\.A-z]+)/.exec(ua)[1];
    
    0 讨论(0)
  • 2020-11-22 00:15

    navigator.sayswho= (function(){
        var ua= navigator.userAgent, tem, 
        M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
        if(/trident/i.test(M[1])){
            tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
            return 'IE '+(tem[1] || '');
        }
        if(M[1]=== 'Chrome'){
            tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
            if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
        }
        M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
        if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
        return M.join(' ');
    })();
    
    console.log(navigator.sayswho); // outputs: `Chrome 62`

    0 讨论(0)
提交回复
热议问题