2

I know that jQuery.fn.jquery gives me 1.10.2, and I want to use .on binding for anything at least jQuery version 1.7.

What I'm doing is:

var jQueryFloat = parseFloat(jQuery.fn.jquery); // 1.1
if (jQueryFloat >= 1.7) {
  // do jQuery 1.7 and above specific code
}

1.1 is less than 1.7, so my jQuery 1.7 and above code does not get run.

Any ideas on another way to figure out if my current version of jQuery is greater than or equal to 1.7? Thanks.

Ian Davis
  • 19,091
  • 30
  • 85
  • 133
  • http://stackoverflow.com/questions/6973941/how-to-check-if-jquery-is-loaded-and-what-version – Glyn Jackson Oct 28 '13 at 17:44
  • Not a duplicate... if you read the question it is different. The asker knows how to get the jQuery version. – DigitalZebra Oct 28 '13 at 17:46
  • @OP, Why wouldn't it be a good way for you? – Alexander Oct 28 '13 at 17:46
  • [Checking the jquery version using a regex.](http://stackoverflow.com/questions/1073423/jquery-plugin-check-version) – Mike Oct 28 '13 at 17:47
  • I'm sure you've thought of this, but why not split the version along the decimal and check both the major is greater than or equal to 1 and the minor version is greater than or equal to 7. – Jace Rhea Oct 28 '13 at 17:49
  • If you need backwards compatibility, I recommend branching the project and then making the necessary changes. Otherwise, I wouldn't recommend bothering to support older versions of jQuery. – zzzzBov Oct 28 '13 at 17:51
  • You should really use feature detection, rather than trying to parse the version. Check if $.fn.on is truth-y (see my answer below) – Adam Jenkins Oct 28 '13 at 17:56

2 Answers2

12

If you are just looking to use .on() if possible, then don't try and parse the version - use feature detection.

var iCanUseOn = !!$.fn.on;

if(iCanUseOn) {

} else {

}

If you want to use more than just .on(), then that's fine, too. Come up with flags for each feature you want to use, but parsing the version is not a good way to solve this problem.

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
6

Use as below.

var arr = $.fn.jquery.split('.'); //["1", "10", "2"]

if(arr[0] > 1 || (arr[0] == 1 && arr[1] > 7))
{
    //Do your thing
}

The above condition will work for future versions like 2.0.1 etc.

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56