How can you detect the version of a browser?

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

    I have made a script in ASP code to detect browser, browser version, OS and OS version. The reason for me to do this in ASP was because i want to store the data in a log-database. So I had to detect the browser serverside.

    Here is the code:

    on error resume next
    ua = lcase(Request.ServerVariables("HTTP_USER_AGENT"))
    moz = instr(ua,"mozilla")  
    ffx = instr(ua,"firefox")  
    saf = instr(ua,"safari")
    crm = instr(ua,"chrome") 
    max = instr(ua,"maxthon") 
    opr = instr(ua,"opera")
    ie4 = instr(ua,"msie 4") 
    ie5 = instr(ua,"msie 5") 
    ie6 = instr(ua,"msie 6") 
    ie7 = instr(ua,"msie 7") 
    ie8 = instr(ua,"trident/4.0")
    ie9 = instr(ua,"trident/5.0")
    
    if moz>0 then 
        BrowserType = "Mozilla"
        BrVer = mid(ua,moz+8,(instr(moz,ua," ")-(moz+8)))
    end if
    if ffx>0 then 
        BrowserType = "FireFox"
        BrVer = mid(ua,ffx+8)
    end if
    if saf>0 then 
        BrowserType = "Safari"
        BrVerPlass = instr(ua,"version")
        BrVer = mid(ua,BrVerPlass+8,(instr(BrVerPlass,ua," ")-(BrVerPlass+8)))
    end if
    if crm>0 then 
        BrowserType = "Chrome"
        BrVer = mid(ua,crm+7,(instr(crm,ua," ")-(crm+7)))
    end if
    if max>0 then 
        BrowserType = "Maxthon"
        BrVer = mid(ua,max+8,(instr(max,ua," ")-(max+8)))
    end if
    if opr>0 then 
        BrowserType = "Opera"
        BrVerPlass = instr(ua,"presto")
        BrVer = mid(ua,BrVerPlass+7,(instr(BrVerPlass,ua," ")-(BrVerPlass+7)))
    end if
    if ie4>0 then 
        BrowserType = "Internet Explorer"
        BrVer = "4"
    end if
    if ie5>0 then 
        BrowserType = "Internet Explorer"
        BrVer = "5"
    end if
    if ie6>0 then 
        BrowserType = "Internet Explorer"
        BrVer = "6"
    end if
    if ie7>0 then 
        BrowserType = "Internet Explorer"
        BrVer = "7"
    end if
    if ie8>0 then 
        BrowserType = "Internet Explorer"
        BrVer = "8"
        if ie7>0 then BrVer = BrVer & " (in IE7 compability mode)"
    end if
    if ie9>0 then 
        BrowserType = "Internet Explorer"
        BrVer = "9"
        if ie7>0 then BrVer = BrVer & " (in IE7 compability mode)"
        if ie8>0 then BrVer = BrVer & " (in IE8 compability mode)"
    end if
    
    OSSel = mid(ua,instr(ua,"(")+1,(instr(ua,";")-instr(ua,"("))-1)
    OSver = mid(ua,instr(ua,";")+1,(instr(ua,")")-instr(ua,";"))-1)
    
    if BrowserType = "Internet Explorer" then
        OSStart = instr(ua,";")
        OSStart = instr(OSStart+1,ua,";")        
        OSStopp = instr(OSStart+1,ua,";")
        OSsel = mid(ua,OSStart+2,(OSStopp-OSStart)-2)
    end if
    
        Select case OSsel
            case "windows nt 6.1"
                OS = "Windows"
                OSver = "7"
            case "windows nt 6.0"
                OS = "Windows"
                OSver = "Vista"
            case "windows nt 5.2"
                OS = "Windows"
                OSver = "Srv 2003 / XP x64"
            case "windows nt 5.1"
                OS = "Windows"
                OSver = "XP"
            case else
                OS = OSSel
        End select
    
    Response.write "<br>" & ua & "<br>" & BrowserType & "<br>" & BrVer & "<br>" & OS & "<br>" & OSver & "<br>"
    
    'Use the variables here for whatever you need........
    
    0 讨论(0)
  • 2020-11-22 00:05

    Adding my own implementation of Hermann's answer. I needed OS detection so that's been added. Also includes some ES6 code (because we have a transpiler) that you might need to ES5-ify.

    detectClient() {
        let nav = navigator.appVersion,
            os = 'unknown',
            client = (() => {
                let agent = navigator.userAgent,
                    engine = agent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
                    build;
    
                if(/trident/i.test(engine[1])){
                    build = /\brv[ :]+(\d+)/g.exec(agent) || [];
                    return {browser:'IE', version:(build[1] || '')};
                }
    
                if(engine[1] === 'Chrome'){
                    build = agent.match(/\bOPR\/(\d+)/);
    
                    if(build !== null) {
                        return {browser: 'Opera', version: build[1]};
                    }
                }
    
                engine = engine[2] ? [engine[1], engine[2]] : [navigator.appName, nav, '-?'];
    
                if((build = agent.match(/version\/(\d+)/i)) !== null) {
                    engine.splice(1, 1, build[1]);
                }
    
                return {
                  browser: engine[0],
                  version: engine[1]
                };
            })();
    
        switch (true) {
            case nav.indexOf('Win') > -1:
                os = 'Windows';
            break;
            case nav.indexOf('Mac') > -1:
                os = 'MacOS';
            break;
            case nav.indexOf('X11') > -1:
                os = 'UNIX';
            break;
            case nav.indexOf('Linux') > -1:
                os = 'Linux';
            break;
        }        
    
        client.os = os;
        return client;
    }
    

    Returns: Object {browser: "Chrome", version: "50", os: "UNIX"}

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

    For any PWA application using angular you can put the code to check if browser is supported or not in body section of index.html -

    <body>
        <div id="browser"></div>
        <script>
            var operabrowser = true;
            operabrowser = (navigator.userAgent.indexOf('Opera Mini') > -1);
            if (operabrowser) {
                txt = "<p>Browser not supported use different browser...</p>";
                document.getElementById("browser").innerHTML = txt;
            }
        </script>
    </body>
    
    0 讨论(0)
  • 2020-11-22 00:07

    I wrote a version detector based on Hermann Ingjaldsson's answer, but more robust and which returns an object with name/version data in it. It covers the major browsers but I don't bother with the plethora of mobile ones and minor ones:

    function getBrowserData(nav) {
        var data = {};
    
        var ua = data.uaString = nav.userAgent;
        var browserMatch = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
        if (browserMatch[1]) { browserMatch[1] = browserMatch[1].toLowerCase(); }
        var operaMatch = browserMatch[1] === 'chrome';
        if (operaMatch) { operaMatch = ua.match(/\bOPR\/([\d\.]+)/); }
    
        if (/trident/i.test(browserMatch[1])) {
            var msieMatch = /\brv[ :]+([\d\.]+)/g.exec(ua) || [];
            data.name = 'msie';
            data.version = msieMatch[1];
        }
        else if (operaMatch) {
            data.name = 'opera';
            data.version = operaMatch[1];
        }
        else if (browserMatch[1] === 'safari') {
            var safariVersionMatch = ua.match(/version\/([\d\.]+)/i);
            data.name = 'safari';
            data.version = safariVersionMatch[1];
        }
        else {
            data.name = browserMatch[1];
            data.version = browserMatch[2];
        }
    
        var versionParts = [];
        if (data.version) {
            var versionPartsMatch = data.version.match(/(\d+)/g) || [];
            for (var i=0; i < versionPartsMatch.length; i++) {
                versionParts.push(versionPartsMatch[i]);
            }
            if (versionParts.length > 0) { data.majorVersion = versionParts[0]; }
        }
        data.name = data.name || '(unknown browser name)';
        data.version = {
            full: data.version || '(unknown full browser version)',
            parts: versionParts,
            major: versionParts.length > 0 ? versionParts[0] : '(unknown major browser version)'
        };
    
        return data;
    };
    

    It can then be used like this:

    var brData = getBrowserData(window.navigator || navigator);
    console.log('name: ' + brData.name);
    console.log('major version: ' + brData.version.major);
    // etc.
    
    0 讨论(0)
  • 2020-11-22 00:07

    This is a Update on Fzs2 & kennebec For New Edge Chromium

    function get_browser() {
        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(/\bEdg\/(\d+)/)
            if(tem!=null)   {return {name:'Edge(Chromium)', version:tem[1]};}
            tem=ua.match(/\bOPR\/(\d+)/)
            if(tem!=null)   {return {name:'Opera', version:tem[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: M[1]
        };
     }
    
    var browser=get_browser(); // browser.name = 'Edge(Chromium)'
                               // browser.version = '86'
    
    console.log(browser);

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

    This page seems to have a pretty nice snippet which only uses the appString and appVersion property as a last resort as it claims them to be unreliable with certain browsers. The code on the page is as follows:

    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName  = navigator.appName;
    var fullVersion  = ''+parseFloat(navigator.appVersion); 
    var majorVersion = parseInt(navigator.appVersion,10);
    var nameOffset,verOffset,ix;
    
    // In Opera 15+, the true version is after "OPR/" 
    if ((verOffset=nAgt.indexOf("OPR/"))!=-1) {
     browserName = "Opera";
     fullVersion = nAgt.substring(verOffset+4);
    }
    // In older Opera, the true version is after "Opera" or after "Version"
    else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
     browserName = "Opera";
     fullVersion = nAgt.substring(verOffset+6);
     if ((verOffset=nAgt.indexOf("Version"))!=-1) 
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In MSIE, the true version is after "MSIE" in userAgent
    else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
     browserName = "Microsoft Internet Explorer";
     fullVersion = nAgt.substring(verOffset+5);
    }
    // In Chrome, the true version is after "Chrome" 
    else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
     browserName = "Chrome";
     fullVersion = nAgt.substring(verOffset+7);
    }
    // In Safari, the true version is after "Safari" or after "Version" 
    else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
     browserName = "Safari";
     fullVersion = nAgt.substring(verOffset+7);
     if ((verOffset=nAgt.indexOf("Version"))!=-1) 
       fullVersion = nAgt.substring(verOffset+8);
    }
    // In Firefox, the true version is after "Firefox" 
    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
     browserName = "Firefox";
     fullVersion = nAgt.substring(verOffset+8);
    }
    // In most other browsers, "name/version" is at the end of userAgent 
    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
              (verOffset=nAgt.lastIndexOf('/')) ) 
    {
     browserName = nAgt.substring(nameOffset,verOffset);
     fullVersion = nAgt.substring(verOffset+1);
     if (browserName.toLowerCase()==browserName.toUpperCase()) {
      browserName = navigator.appName;
     }
    }
    // trim the fullVersion string at semicolon/space if present
    if ((ix=fullVersion.indexOf(";"))!=-1)
       fullVersion=fullVersion.substring(0,ix);
    if ((ix=fullVersion.indexOf(" "))!=-1)
       fullVersion=fullVersion.substring(0,ix);
    
    majorVersion = parseInt(''+fullVersion,10);
    if (isNaN(majorVersion)) {
     fullVersion  = ''+parseFloat(navigator.appVersion); 
     majorVersion = parseInt(navigator.appVersion,10);
    }
    
    document.write(''
     +'Browser name  = '+browserName+'<br>'
     +'Full version  = '+fullVersion+'<br>'
     +'Major version = '+majorVersion+'<br>'
     +'navigator.appName = '+navigator.appName+'<br>'
     +'navigator.userAgent = '+navigator.userAgent+'<br>'
    )
    
    0 讨论(0)
提交回复
热议问题