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.
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.
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
or
Try this:
Split the jQuery version string into numbers to test their value. Some version strings do not contain an increment, so make them 0.
Use the jQuery error function to prevent execution with an explanation to the developer.
I feel like all the solutions are rather bulky, is mine below missing something?
Compressed version:
Long version for clarity: