How to compare browser versions via javascript?
For example if script wants to know if user is using Firefox 3.5+
Is there any way?
You should employ object/feature detection to ensure that something will work before you apply it. From that link:
While browser detection works well enough for 90% of your visitors, some obscure browsers won't be treated correctly and browsers that appear after you've written the page may not be adequately covered either. The results would be either a stream of error messages or a script that isn't called while the browser can easily handle it. In both cases, you're cheating your end users and coding incorrectly.
To get the browser version, you can use the Navigator object:
alert(navigator.appName + ' ' + navigator.appVersion);
navigator.appVersion is equal to "5.0 (Windows)" – definitely not the version of FireFoxThis fails now with Firefox getting to 10.0. Come up with a different version:
// Takes 2 strings "2.3.4.5" and "2.5" and returns less, equal and greater than 0
// depending which one is bigger
function compareVersions(a, b) {
var v1 = a.split('.');
var v2 = b.split('.');
for(var i = 0; i < Math.min(v1.length, v2.length); i++) {
var res = v1[i] - v2[i];
if (res != 0)
return res;
}
return 0;
}
Math.min, you should maybe try 0 filling the shorter one to find the differenceParse version strings as float type, then compare them with if statement or whatever you want.
Here is a sample I've used for checking IE browser version:
if(parseFloat($.browser.version) <= 8.0)
{
// Do whatever you want if browser version lower than equal to 8.0
}
else
{
// Do whatever you want if browser greater than 8.0
}
jQuery.browser was deprecated in jQuery 1.3 and removed in 1.9 (see api.jquery.com/jQuery.browser for details)
quirksmode.orghas an entry on that. It's pretty detailed: quirksmode.org/js/detect.html