1

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

SidOfc
  • 4,552
  • 3
  • 27
  • 50
katie hudson
  • 2,765
  • 13
  • 50
  • 93
  • 1
    Possible duplicate of [How to detect Safari, Chrome, IE, Firefox and Opera browser?](http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser) – Stphane Dec 15 '15 at 10:25
  • Or possible duplicate of [How can you detect the version of a browser?](http://stackoverflow.com/questions/5916900/how-can-you-detect-the-version-of-a-browser) – Sami Dec 15 '15 at 10:29
  • These links help a lot. My question really comes down to the effeciency, as I do not think all of these if statements are the best, Is there any way to improve this? – katie hudson Dec 15 '15 at 10:39

2 Answers2

3

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
Adriaan Meuris
  • 351
  • 2
  • 12
0

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;
}
DinP
  • 59
  • 7