Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if jquery version is greater than 1.7?

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.

like image 819
Ian Davis Avatar asked Jan 21 '26 03:01

Ian Davis


2 Answers

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.

like image 149
Adam Avatar answered Jan 23 '26 19:01

Adam


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.

like image 30
Venkata Krishna Avatar answered Jan 23 '26 19:01

Venkata Krishna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!