简体   繁体   中英

Javascript - detecting browser version

I know this is not the most reliable way, but I don't really have many other choices. I essentially need to detect what browser and version visits my website. If it is a certain browser/version, it will display a message. This is for the TLS issue, whereby certain browser versions will not display a https page.

Anyways, at the moment I have something like the following

function getBrowserInfo()
{
    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(' ');
}

var browserInfo = getBrowserInfo();
browserInfo = browserInfo.split(" ");

$(function () {
    alert(browserInfo);
    if(browserInfo[0] == 'Firefox' && browserInfo[1] < 27) {
        alert("Firefox");
    }
    if(browserInfo[0] == 'Chrome' && browserInfo[1] < 22) {
        alert("Chrome");
    }
    if(browserInfo[0] == 'IE' && browserInfo[1] < 11) {
        alert("IE");
    }
    if(browserInfo[0] == 'Safari' && browserInfo[1] < 11) {
        alert("Safari");
    }
    if(browserInfo[0] == 'Opera' && browserInfo[1] < 14) {
        alert("Safari");
    }
});

One thing that concerns me is the splitting of the string, incase a browser returns a different format. Additionally, I think all of these if statements are redundant, but I cant think of a more efficient way to do this.

Essentially, I am looking for advice as to the best way to detect a browser, and its version, so I can display a custom message for that browser.

Thanks

It seems like you're using jQuery. You could detect the browser version using $.browser, but that feature is deprecated starting from version 1.9.1.

I would advise using this jQuery-plugin, which supports browser type & version detection: https://github.com/gabceb/jquery-browser-plugin

Using this plugin, you can test for browser vendors using this code:

if($.browser.msie) {
  alert('ie');
}

if($.browser.mozilla) {
  alert('firefox');
}

...

If you would like to check for the version of the browser, you could use this code:

$.browser.version

Return ie version using javascript

function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') 
{
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM