Domanda

I'm trying to check if the current jquery version is greater than 1.8.3 but parseInt($.fn.jquery) only outputs one digit.

È stato utile?

Soluzione

Not sure it's the most efficient but seems to work... Take the version string, split it into tokens and test each token, like below

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

Altri suggerimenti

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

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

Here's a simplistic "one liner" approach. I pad out the digits with leading zeros.:

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

I use this snipped:

/**
* 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;
}

So you can use it like this:

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

You have to keep also an eye on versions beyond 9.99.99. The code is also expandable for patterns like 11.11.11.11. It will be also worth a tought to check if the array-values are valid-integer. You can go further to check also for notations like: 11.11.11.RC1

Hope it helps, sorry for my english

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top