Craig
Craig

Reputation: 1225

Javascript Switch statement not providing the correct result

I am attempting to detect the browser and browser version for a web application

The code for determining the browser is -

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 'IE ' + (tem[1] || '');
    }
    if (M[1] === 'Chrome') {
        tem = ua.match(/\bOPR\/(\d+)/)
        if (tem != null) { return 'Opera ' + 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 M[0];
}

The code for determining the version is -

function get_browser_version() {
    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(/\bOPR\/(\d+)/)
        if (tem != null) { return 'Opera ' + 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 M[1];
}

The rest of the code is -

var browser = get_browser().toLowerCase();
var browser_version = get_browser_version();

alert(browser, browser_version);

switch (browser) {
    case 'safari':
        if (browser_version < 5) {
            $('#BrowserVersionModal').modal('show');
        }
        break;
    case 'firefox':
        if (browser_version < 28) {
            $('#BrowserVersionModal').modal('show');
        }
        break;
    case 'ie':
        if (browser_version < 9) {
            $('#BrowserVersionModal').modal('show');
        }
        break;
    case 'opera':
        if (browser_version < 6) {
            $('#BrowserVersionModal').modal('show');
        }
        break;
    default:

}

The alert indicates that the browser is ie and the version is 11 but I never see the modal being displayed. If I add an alert in the case statement for ie, it never is shown either. What am I missing?

Upvotes: 0

Views: 93

Answers (4)

dehrg
dehrg

Reputation: 1741

You have multiple issues here.

return 'IE ' + (tem[1] || ''); in the browser check doesn't give you something that will match 'ie' in the case sensitive switch statement.

The other issue is that browser_version < 9 will not evaluate to true if the version is 11.

Upvotes: 2

vp_arth
vp_arth

Reputation: 14992

Try followed;

var versions = {
  ie: 9,
  opera: 6,
  firefox: 28,
  safari: 5
};

if(!versions[browser]) {
  alert('Unknown browser: '+browser);
} else if (browser_version < versions[browser]) {
  alert('poor browser');
} else {
  alert('good browser');
}

Upvotes: 0

Bradley Imbach
Bradley Imbach

Reputation: 1

I think because the version is 11, the condition if (browser_version < 9) will be false so the modal will not show.

Upvotes: 0

progsource
progsource

Reputation: 639

With

case 'ie':
    if (browser_version < 9) {
        $('#BrowserVersionModal').modal('show');
    }
    break;

you say - if it is IE and less than 9 - so ie 11 is not triggered, because it is > 9

Upvotes: 0

Related Questions