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:47

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.

查看更多
Juvenile、少年°
3楼-- · 2020-02-10 02:51

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.

查看更多
看我几分像从前
4楼-- · 2020-02-10 02:53

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.
}
查看更多
smile是对你的礼貌
5楼-- · 2020-02-10 02:56

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
}
查看更多
来,给爷笑一个
6楼-- · 2020-02-10 02:56

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;
}
查看更多
爷、活的狠高调
7楼-- · 2020-02-10 02:57

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