Skip to content

Instantly share code, notes, and snippets.

@dshaw
Created October 29, 2010 03:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dshaw/652870 to your computer and use it in GitHub Desktop.
Save dshaw/652870 to your computer and use it in GitHub Desktop.
Determine if a page has the minimum jQuery version you need to do what you're doing.
function minVersion(version) {
var $vrs = window.jQuery.fn.jquery.split('.'),
min = version.split('.');
for (var i=0, len=$vrs.length; i<len; i++) {
console.log($vrs[i], min[i]);
if (min[i] && $vrs[i] < min[i]) {
return false;
}
}
return true;
}
@dshaw
Copy link
Author

dshaw commented Oct 29, 2010

usage:
minVersion('1.4.2')

minVersion('1.4')

minVersion('2')

@groupsky
Copy link

groupsky commented Aug 7, 2013

when jQuery is version 1.10.0, and tests against 1.9.0 it returns false. To fix change 6th line to:

    if (min[i] && (+$vrs[i]) < (+min[i])) {

@budiadiono
Copy link

Hi Dshaw, I tried it wasn't working to tests jQuery version 2.0.3 against 1.9.1 -- I made a fix in my fork: https://gist.github.com/budiadiono/7954617, please have a take a look.

@javamad
Copy link

javamad commented Jun 28, 2015

Hi,

I was trying this but it doesn't work with some combinations ... so I came up with this variation which builds a single number from the major.minor.patch versions and compares that.

(I multiply the major version by 1000 to cater for having minor versions that go beyond 9 )

function minVersion(version) {
//console.log('minVersion:: testing for jquery minimum of ' + version );
if(typeof window.jQuery == 'undefined'){
console.log('minVersion:: no jquery found ...');
return false;
}
var vrs = window.jQuery.fn.jquery.split('.'),
min = version.split('.');
//pad the arrays to 3 digits
while(vrs.length < 3)
vrs.push(0);

    while(min.length < 3)
        min.push(0);
    //build a number
    var req = parseInt(min[0])*1000 + parseInt(min[1])*10 + parseInt(min[2]);
    var ins = parseInt(vrs[0])*1000 + parseInt(vrs[1])*10 + parseInt(vrs[2]);
    //DEBUG:
    console.log('req = ' + req + ' and ins = ' + ins );

    return ins >= req;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment