是否可以在同一页面上加载多个不同版本的jQuery?

问题描述:

我正在制作一个bookmarklet,如果找不到该对象,它将加载jQuery.加载将检查jQuery的版本.代码如下:

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(){

    var myBkl = {
         loadScript: function(src) {
            if(window.jQuery && window.jQuery.fn.jquery == '1.3.2'){
                return;
            }
            var s = document.createElement('script');
            s.setAttribute('src', src);
            s.setAttribute('type', 'text/javascript');
            document.getElementsByTagName('head')[0].appendChild(s); 
        },
        whenLoaded: function(callback){
            if (typeof(window.jQuery) !== 'undefined' && window.jQuery.fn.jquery == '1.3.2') { 
                callback(window.jQuery); 
            } 
            else {
                setTimeout((function() {myBkl.whenLoaded(callback); }), 100);
            } 
        },
        init: function($){
            console.log($.fn.jquery);
        }
    };
    myBkl.loadScript('http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js');
    myBkl.whenLoaded(myBkl.init);

})();

我使用此书签创建器来创建书签 http://subsimple.com/bookmarklets/jsbuilder.htm

I use this bookmarklet builder to create the bookmarklet http://subsimple.com/bookmarklets/jsbuilder.htm

很明显,如果页面已经加载了jQuery. 1.3.2脚本的加载将覆盖页面上的window.jQuery对象.我只是想知道是否有让1.3.2加载到另一个自命名变量的方法?使用jQuery.noConflict(true);吗?

Obviously if the page already has jQuery loaded. The loading of the 1.3.2 script would overwrite the window.jQuery object on the page. I just wonder is there anyway to let 1.3.2 to load to another self named variable? Using jQuery.noConflict(true); ?

我怀疑您已经了解了所有警告,并了解可以将jQuery移至另一个命名空间:

I suspect you've already seen all the caveats and understand you can move jQuery to another namespace:

//Completely move jQuery to a new namespace in another object.

var dom = {};
dom.query = jQuery.noConflict(true);

该插件可能无法正常工作,您必须在加载或使用其他脚本之前完成所有这些工作.

And that plugins probably won't work AND you must do all this before other scripts are loaded or used.

祝你好运/有点好奇要知道这是否对你有用〜

Good Luck / kinda curious to learn if this works for you~