jQuery Plugin Check Version

前端 未结 15 906
轮回少年
轮回少年 2020-12-16 11:57

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

相关标签:
15条回答
  • 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.

    0 讨论(0)
  • 2020-12-16 12:19

    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');
      }
    
    0 讨论(0)
  • 2020-12-16 12:22

    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
    }
    
    0 讨论(0)
  • 2020-12-16 12:25

    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");
    }
    
    0 讨论(0)
  • 2020-12-16 12:25

    Something like that?

    It checks if jQuery is loaded, and if it is, checks the version. This method works correct for 2 digit version numbers too (1.10.28), and if needed you can extend it for 3 digits, if you replace the 100 numbers to 1000...

    function checkJQueryMinVersion(need) {
        var v1 = need.split('.');
        var v1_num = 0;
        var v2 = jQuery.fn.jquery.split('.');
        var v2_num = 0;
    
        if(v1[0] != undefined) {
            v1_num += 100*100*parseInt(v1[0]);
        }
        if(v1[1] != undefined) {
            v1_num += 100*parseInt(v1[1]);
        }
        if(v1[2] != undefined) {
            v1_num += parseInt(v1[2]);
        }
    
        if(v2[0] != undefined) {
            v2_num += 100*100*parseInt(v2[0]);
        }
        if(v2[1] != undefined) {
            v2_num += 100*parseInt(v2[1]);
        }
        if(v2[2] != undefined) {
            v2_num += parseInt(v2[2]);
        }
        return (v1_num <= v2_num);
    }
    if(!window.jQuery || !checkJQueryMinVersion('1.10.28')) {
        alert('Loading //ajax.googleapis.com/ajax/libs/jquery/1.10.28/jquery.min.js');
        var script = document.createElement('script');script.type = 'text/javascript';
        script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.10.28/jquery.min.js';
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    else {
        alert('Jquery version OK');
    }
    
    0 讨论(0)
  • 2020-12-16 12:29

    Try this:

    >>> jQuery.fn.jquery;
    "1.3.2"
    
    0 讨论(0)
提交回复
热议问题