Loading Multiple jQuery Versions

前端 未结 2 322
陌清茗
陌清茗 2021-01-03 06:46

I\'ve written a tool that uses jQuery, and the tool is dynamically loaded on many different web pages, including ones that already use jQuery. I am trying to load jQuery on

相关标签:
2条回答
  • 2021-01-03 07:18

    I have a quite similar issue on my webapp, there is a way to keep the previous version and use your own, even with plugins, here is what I do:

    <!-- USING A NEWER VERSION OF JQUERY -->
    
    <!-- store and protect the old one -->
    <script>
    var oldJQuery = jQuery.noConflict();
    </script>
    
    <!-- load the new version and required plugins -->
    <script type="text/javascript" src="../../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../../script/jquery.mousewheel.min.js"></script>
    <script type="text/javascript" src="../../script/jquery.hammer.js"></script>
    <script type="text/javascript" src="../../script/jquery.xml2json.min.js"></script>
    
    <!-- store and protect the new one and put the old one back -->
    <script>
    var jQuerX = jQuery.noConflict();
    jQuery = oldJQuery;
    </script>
    

    it keep the old version unchanged, and you can use yours under the chosen alias (jQuerX).

    0 讨论(0)
  • 2021-01-03 07:25

    You need to allow the dynamically loaded copy of jQuery to finish loading before calling .noConflict(). Use the onload event on <script>.

    Demo:

    Script:

    var script = document.createElement( 'script' );
    script.onload = function () {
        jQuery.noConflict( true ); //remove this and image load fails
    };
    script.src = 'http://cdn01.dailycaller.com/wp-includes/js/jquery/jquery.js?ver=1.7.2';
    document.head.appendChild(script);
    
    0 讨论(0)
提交回复
热议问题