jQuery Plugin Check Version

前端 未结 15 920
轮回少年
轮回少年 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条回答
  •  暖寄归人
    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');
    }
    

提交回复
热议问题