Is it possible to load multiple different version of jQuery on the same page?

前端 未结 4 1461
天命终不由人
天命终不由人 2020-12-17 03:13

I\'m making a bookmarklet which will load jQuery if the object is not found. The loading will check on the version of jQuery. The code is like:

(function(){
         


        
4条回答
  •  暖寄归人
    2020-12-17 04:04

    When running "jQuery.noConflict(true);" The code that use the first jQuery version may break.
    In some cases this, code doesn't even belong to you. You write a script, which is supposed to be added to pages and which uses jQuery, and you know nothing about the hosting page.
    A hosting code may load its jQuery version, detected that it was loaded, start working with it, then wait (setTimeout) ,and then your code starts, do "jQuery.noConflict(true);" , and waits till it is loaded. While your code waits, the control may return to the hosting code which tries to run its jQuery and finds that it doesn't exist.

    My suggestion, for this case, is to load the jQuery in a different new window, without removing it from the original one. Then, when it is loaded, use the "jQuery.noConflict(true);" on the new window to copy it to the original window. However the new jQuery object is actually running on the new window and its document. So when using the new jQuery the original window.document must be pass as the second parameter like this:

    newJq("#elementInOriginalDocument", window.document).html("some text");
    

    Following my implementation for this idea:

    
        
            
        
        
            Test jQuery
            
    hostScope
    guestScope

提交回复
热议问题