Check if version jQuery 1.8 exceeds?

I am trying to check if the current version of jQuery is greater than 1.8, but parseInt($.fn.jquery)only displays one digit.

+4
source share
4 answers

Not sure if it is the most efficient, but it seems to work ... Take the version string, split it into tokens and test each token as shown below

var vernums = $.fn.jquery.split('.');
if (parseInt(vernums[0]) > 0 && parseInt(vernums[1]) >= 8 && parseInt(vernums[2]) > 3) {
  // Do stuff here
}
+3
source

A slightly shorter test "one liner", valid in version 9.99.99:

$.fn.jquery.replace(/\.(\d)/g,".0$1").replace(/\.0(\d{2})/g,".$1") > "1.08.03"
+2
source

" ". .:

if(jQuery.fn.jquery.split('.')
    .map(function(i){return('0'+i).slice(-2)})
    .join('.') > '01.08.03')
{
    alert('yes');
}
else
{
    alert('no');
}
+1

:

/**
* Checks if versionA is bigger, lower or equal versionB
* It checks only pattern like 1.8.2 or 1.11.0
* Major version, Minor version, patch release
* @param strVersionA a version to compare
* @param strVersionB the other version to compare
* @returns {*} 1 if versionA is bigger than versionB, -1 if versionA is lower than versionB and 0 if both versions are equal
* false if nothing worked
*/
function checkVersion(strVersionA, strVersionB){
    var arrVersionA = strVersionA.split('.');
    var arrVersionB = strVersionB.split('.');
    var intVersionA = (100000000 * parseInt(arrVersionA[0])) + (1000000 * parseInt(arrVersionA[1])) + (10000 * parseInt(arrVersionA[2]));
    var intVersionB = (100000000 * parseInt(arrVersionB[0])) + (1000000 * parseInt(arrVersionB[1])) + (10000 * parseInt(arrVersionB[2]));

    if (intVersionA > intVersionB) {
        return 1;
    }else if(intVersionA < intVersionB){
        return -1;
    }else{
        return 0;
    }
    return false;
}

, :

var blnIsNewJQuery = checkVersion($.fn.jquery,"1.8.3")>0?true:false;

You should also keep track of versions beyond 9.99.99. The code also extends for templates such as 11/11/11. It is also worth checking if the values ​​of the valid-integer array are valid. You can go further to check also such notations as: 11.11.11.RC1

Hope this helps, sorry for my english

+1
source

All Articles