jQuery: noConflict

后端 未结 8 2131
無奈伤痛
無奈伤痛 2020-12-01 12:25

I just cannot work this out. My god it\'s making my brain hurt, so I turn to you, the good people of the internet.

I\'ve been staring at the documentation for the jQ

8条回答
  •  清歌不尽
    2020-12-01 12:55

    WAY late .... but might help someone - threw this together.

    /**
    *********************************************************************************************************
    *           jQuery version check
    *           $.noConflict() is not enough to ensure jQuery as well as $ 
    *           has correct version (can cause troubles with jQuery plugins that use fn(){}(jQuery);
    *
    *           useage: restoreJquery('1.10.2',true) //for unminimized dev version
    *                   restoreJquery('1.10.2')          //for minimized version
    *                   restoreJquery('1.10.2',true,myFunction)          //for minimized version, that executes a function once jQuery is in the page
    *
    *
    **/
    
    function restoreJquery(wantedVersion,devVersion,callback) {
        if(!wantedVersion){
            wantedVersion= '1.10.2';
        }
    
        //is current jQuery version ===wanted version? (double check to avoid undefined errors)
        if($ && $.fn.jquery!==wantedVersion){                
    
            //if not invoke noConflict with true (true=gives up the jQuery name space aswell as $)
            var $jQuery = jQuery.noConflict(true); 
            //Line Assign the new object to new instantiated versions of jQuery
            var $=jQuery=$jQuery;                          
          }
    
        //if jQuery is still not the correct version inject it into page via plain vanilla js
    
        //if $ does not exist or the version is wrong inject it into the page
        if(!$ || $.fn.jquery!==wantedVersion){ 
    
            var head=document.getElementsByTagName('head')[0], //get Head
                jqPath=wantedVersion+'jquery.', // ex '1.10.2/jquery.
                        script=document.createElement('script'); //create empty scripttag
    
                if(devVersion){  //minimized or not?
                    jqPath+='min.js';
                }else{
                    jqPath+='js';
                }
    
            script.src='//http://ajax.googleapis.com/ajax/libs/jquery/'+jqPath; //create src path
            script.type='text/javascript'; //add type
            script.async=true; //set async to true
            //real browsers
            script.onload=callback;  //call callback on load
    
            //Internet explorer doesnt support onload on script (typical ie)
            script.onreadystatechange = function() {
                if (this.readyState == 'complete') {
                    callback();
                }
            }
    
    
        head.appendChild(script);
    
        }else{
            //otherwise call the callback since noConflict solved our jQuery versioning issues
            callback();
        }
    
    };
    

提交回复
热议问题