How can you detect the version of a browser?

后端 未结 28 2617
佛祖请我去吃肉
佛祖请我去吃肉 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:19

    I use this to get de Name and number (int) of the version of the actual browser:

    function getInfoBrowser() {
        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: 'Explorer', version: parseInt((tem[1] || '')) };
        }
        if (M[1] === 'Chrome') {
            tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
            if (tem != null) { let app = tem.slice(1).toString().split(','); return { name: app[0].replace('OPR', 'Opera'), version: parseInt(app[1]) }; }
        }
        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: parseInt(M[1])
        };
    }
    
    function getBrowser(){
      let info = getInfoBrowser();
      $("#i-name").html(info.name);
      $("#i-version").html(info.version);
    }
    
    
    
    
    Name:
    Version:

    This run in

    Chrome ; Firefox ; Safari ; Internet Explorer (>= 9) ; Opera ; Edge

    For me.

提交回复
热议问题