jQuery Plugin Check Version

2020-02-10 02:18发布

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.

15条回答
淡お忘
2楼-- · 2020-02-10 02:41

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.

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-10 02:42

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

查看更多
贪生不怕死
4楼-- · 2020-02-10 02:43
$().jquery;

or

jQuery.fn.jquery;
查看更多
做个烂人
5楼-- · 2020-02-10 02:44

Try this:

>>> jQuery.fn.jquery;
"1.3.2"
查看更多
Anthone
6楼-- · 2020-02-10 02:44

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.

查看更多
The star\"
7楼-- · 2020-02-10 02:47

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");
}
查看更多
登录 后发表回答