21

When writing a new jQuery plugin is there a straightforward way of checking that the current version of jQuery is above a certain number? Displaying a warning or logging an error otherwise.

It would be nice to do something like:

jQuery.version >= 1.2.6

in some form or another.

ROGUE
  • 571
  • 2
  • 5
  • 15
  • Roguepixel, you should unaccept the answer below, if StackOverflow’s stupid freezes do not apply. It is incredibly brittle, does not generalize well, and is absolutely the Wrong Way to do things. – Alan H. May 25 '12 at 23:44

15 Answers15

12

Here is a check I used to make sure the user was using at least v. 1.3.2 of jQuery.

if (/1\.(0|1|2|3)\.(0|1)/.test($.fn.jquery) 
                    || /^1.1/.test($.fn.jquery) 
                    || /^1.2/.test($.fn.jquery)) 
{
    //Tell user they need to upgrade.
}
RedWolves
  • 10,379
  • 12
  • 49
  • 68
  • Thanks to everyone who left answers, I guess there's no easy way to check it because of it being a String, I think the above solution is the most appropriate though. Cheers – ROGUE Jul 02 '09 at 10:01
  • 1
    http://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number – ripper234 Mar 13 '12 at 10:51
  • 1
    Technically jQuery `1.0.4` (which exists) would pass your test. I don’t think regexes are a good or intuitive solution here. An actual version comparison function (e.g., split on periods, then compare each part by value) would be much more reliable — not to mention future-proof for visitors here to test for version 1.4.0 or 1.7.3 or whatever! – Alan H. May 25 '12 at 23:42
  • 2
    Not to mention this code will tell a user of jQuery `1.10.0`, whenever that comes out, they they need to “upgrade”. – Alan H. May 25 '12 at 23:43
10

Try this:

>>> jQuery.fn.jquery;
"1.3.2"
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
3

I feel like all the solutions are rather bulky, is mine below missing something?

Compressed version:

(parseInt(jQuery.fn.jquery.split('.').join('')) > 140) ? alert("Running jquery greater than 1.4.0") : alert("current jquery version is 1.4.0 or less");

Long version for clarity:

// get version as a string and get rid of the periods. 
version = jQuery.fn.jquery.split('.').join('');

// Make into one long number for easy comparison.  Example, 171, or 141.
version = parseInt(version);
if(version > 141){
    alert("We're using a version greater than 1.4.1");
}else{
    alert("jQuery version is 1.4.1 or lower");
}
David Hobs
  • 4,351
  • 2
  • 21
  • 22
  • I really prefer this one. Most of the solutions are either just telling the version that is no way a solution if question is read properly (I wonder who have rated them up) and some miss greater than check and just work on equality. – Usman Zaheer May 18 '12 at 09:26
  • 1
    It might work in practice currently, but by removing periods, you are discarding important information. Imagine jQuery version 1.0.12, which doesn’t and probably will never exist — certainly 1012 > 140, but 1.0.12 < 1.4.0. – Alan H. May 25 '12 at 23:52
3

See my answer here: jQuery Version Compatibility Detection

Community
  • 1
  • 1
kflorence
  • 2,187
  • 2
  • 16
  • 15
  • 1
    Superb! It's one long piece of code, but you really did account for everything. I'll have to save that. Thanks! – David Hobs Jun 09 '12 at 05:47
2
$().jquery;

or

jQuery.fn.jquery;
rahul
  • 184,426
  • 49
  • 232
  • 263
1

Pass version as array of integers, like jQmin([1,4,2]) to check if the current version is equal or above v1.4.2.

function jQmin(min) {
    var i, cur, 
        cver = $.fn.jquery, 
        c = cver.split(".");
    while (min.length < c.length) min.push(0);

    for (i = 0; i < min.length; i++) {
        cur = parseInt(c[i]);
        if (cur > min[i]) return true;
        if (cur < min[i]) return false;
    }
    return (cver === min.join("."));
}
1

What about:

function minVersion(version) {
var $vrs = window.jQuery.fn.jquery.split('.'),
min  = version.split('.'),
prevs=[];

for (var i=0, len=$vrs.length; i<len; i++) {
    console.log($vrs[i], min[i], prevs[i-1]);
    if (min[i] && $vrs[i] < min[i]) {
        if (!prevs[i-1] || prevs[i-1] == 0)
            return false;
    } else {
        if ($vrs[i] > min[i])
            prevs[i] = 1;
        else
            prevs[i] = 0;
    }
}
return true;

}

I wrote that code on my forked gist: https://gist.github.com/budiadiono/7954617, original code written by dshaw on: https://gist.github.com/dshaw/652870.

Adiono
  • 1,034
  • 11
  • 19
1

If it's enough to check minor version, not patch version, you can do something like

parseFloat(jQuery.fn.jquery) >= 1.9

It is possible to change this slightly to check >= 1.4.3: parseFloat(jQuery.fn.jquery) > 1.4 || parseFloat(jQuery.fn.jQuery) == 1.4 && parseFloat(jQuery.fn.jQuery.slice(2)) >= 4.3

0

since there is no jquery version that us more than one digit, you can safely use

if ([$.fn.jquery,"1.6"].sort()[0] == "1.6")
{
  // jquery version greater or same as 1.6
}
else
{
  // jquery version lower than 1.6
}
gabri.ns
  • 9
  • 2
  • 2
    “yet”. Please don’t do this. This is exactly the kind of thinking that forced Opera to report its version as version 9 when it it reached version 10 and 11 — people assuming one digit. – Alan H. May 25 '12 at 23:39
0

Found this in jquery.1.3.2 source:

 jQuery.fn = jQuery.prototype = {
    init: function( selector, context ) {
    .....
    // The current version of jQuery being used
    jquery: "1.3.2",

I didn't check, but something like "$.fn.jQuery" might work.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
0

The only correct answer is to use a version comparison function.

A lot of the answers here make really poor assumptions that even a little critical thinking can show to be incorrect, like assuming that each sub-version is one digit, or that jQuery will never reach version 2. I do not want to re-hash them in this answer, but those periods have meaning: It means “whatever is on my left is a number that matters more than everything to my right.” And therefore the correct solution always checks the leftmost version number component, and only checks the next component if the first pair matched exactly, and so on.

jQuery’s version number is available in $.fn.jquery as noted in many other answers here. It is a string, and your version comparison function should expect strings.

Here is a port of PHP’s version comparison function to JS. Note it’s probably overkill for jQuery, especially if you assume you’ll only ever see numbers and won’t be dealing with release candidates and the like. But it’s going to be reliable and future-proof.

Alan H.
  • 16,219
  • 17
  • 80
  • 113
0

Instead of using parseInt as i saw in one of the answers above i would suggest to use parseFloat as mentioned below

  var _jQueryVer = parseFloat('.'+$().jquery.replace(/\./g, ''));
  /* Here the value of _jQueryVer would be 0.1012 if the jQuery version is 1.0.12
     which in case of parseInt would be 1012 which is higher than 
     140 (if jQuery version is 1.4.0) 
  */
  if(_jQueryVer < 0.130) { 
    alert('Please upgrade jQuery to version 1.3.0 or higher');
  }
рüффп
  • 5,172
  • 34
  • 67
  • 113
  • Changing the "throw out the dots" approach to use floats instead of ints doesn't change much; you are still discarding valuable information. Imagine comparing `1.2.3` vs `1.11.0`, which in this method becomes `0.123` vs `0.111`, incorrectly "determining" that `1.2` would be newer than `1.11`. – Alan H. Aug 10 '16 at 21:48
0

Something like that?

It checks if jQuery is loaded, and if it is, checks the version. This method works correct for 2 digit version numbers too (1.10.28), and if needed you can extend it for 3 digits, if you replace the 100 numbers to 1000...

function checkJQueryMinVersion(need) {
    var v1 = need.split('.');
    var v1_num = 0;
    var v2 = jQuery.fn.jquery.split('.');
    var v2_num = 0;

    if(v1[0] != undefined) {
        v1_num += 100*100*parseInt(v1[0]);
    }
    if(v1[1] != undefined) {
        v1_num += 100*parseInt(v1[1]);
    }
    if(v1[2] != undefined) {
        v1_num += parseInt(v1[2]);
    }

    if(v2[0] != undefined) {
        v2_num += 100*100*parseInt(v2[0]);
    }
    if(v2[1] != undefined) {
        v2_num += 100*parseInt(v2[1]);
    }
    if(v2[2] != undefined) {
        v2_num += parseInt(v2[2]);
    }
    return (v1_num <= v2_num);
}
if(!window.jQuery || !checkJQueryMinVersion('1.10.28')) {
    alert('Loading //ajax.googleapis.com/ajax/libs/jquery/1.10.28/jquery.min.js');
    var script = document.createElement('script');script.type = 'text/javascript';
    script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.10.28/jquery.min.js';
    document.getElementsByTagName('head')[0].appendChild(script);
}
else {
    alert('Jquery version OK');
}
Tommy at LIW
  • 864
  • 6
  • 4
0

I'm surprised no one has given this solution yet:

//Returns < 0 if version1 is less than version2; > 0 if version1 is greater than version2, and 0 if they are equal.
var compareVersions = function (version1, version2){

    if(version1 == version2)return 0;

    var version1Parts = version1.split('.');
    var numVersion1Parts = version1Parts.length; 

    var version2Parts = version2.split('.');
    var numVersion2Parts = version2Parts.length; 


    for(var i = 0; i < numVersion1Parts && i < numVersion2Parts; i++){
        var version1Part = parseInt(version1Parts[i], 10);
        var version2Part = parseInt(version2Parts[i], 10);
        if(version1Part > version2Part){
            return 1;
        }
        else if(version1Part < version2Part){
            return -1;
        }
    }

    return numVersion1Parts < numVersion2Parts ? -1 : 1;
}
DiverseAndRemote.com
  • 19,314
  • 10
  • 61
  • 70
0

Split the jQuery version string into numbers to test their value. Some version strings do not contain an increment, so make them 0.

var
  version = $.fn.jquery.split('.'),
  versionMinor = parseFloat(version[1]),
  versionIncrement = parseFloat(version[2] || '0');

if (versionMinor === 4 && versionIncrement < 3 || versionMinor < 4) {
  $.error('Requires jQuery 1.4.3 or higher to support ...');
}

Use the jQuery error function to prevent execution with an explanation to the developer.

Jamie
  • 507
  • 4
  • 8
  • This is a good beginning of an answer! You don’t assume that all version number sections are one digit, and you don’t attempt to use the entirely wrong tool for the job (e.g. regexes like RedWolves), but then your version check is pretty bad. It’s very difficult to follow, and straight-up incorrect: It errors whenever versionMinor is less than four, so it will explode on jQuery 2.0.0, for example. – Alan H. May 25 '12 at 23:50