3

我有一个加载这个脚本的 boolmarklet,它加载 jQuery。我尽量避免与网站上任何已加载的 jQuery 发生冲突。

当我这样做时:

/* Loading jquery and making sure we don't enter in conflict with
another js lib or another jquery */
var oldJquery;

if (typeof jQuery !== "undefined") { 
    log('jQuery is present. Setting noConflict() on', jQuery().jquery)
    oldJquery = jQuery.noConflict(true); 
} else {
    oldJquery= false;
}

log('Starting to load jQuery')
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://code.jquery.com/jquery-1.8.3.min.js?' + (new Date().getTime());
script.onreadystatechange = function() {
    setTimeout(function(){
    log('jQuery is loaded. Starting BM with', $().jquery)
    bookmarklet(jQuery.noConflict(true))
    if (oldJquery){
        log('Restore old jQuery reference')
        jQuery = oldJquery;
        $ = jQuery;
    }
    }, 4000)

};
script.onload = script.onreadystatechange
head.appendChild(script);

我最终得到这个输出:

############# RESTART #############

jQuery is present. Setting noConflict() on1.4.4
Starting to load jQuery
jQuery is loaded. Starting BM with 1.4.4
Scratchpad (ligne 312)
BM is started with jquery version 1.4.4
Scratchpad (ligne 27)
Display alert: Please wait...
Load view jQuery viewport plugin and wait for DOM ready
Restore old jQuery reference

为什么在“BM is started with jquery version”中没有 I 1.8.2 作为 jquery 版本?

我尝试了各种组合都($|jQuery).noConflict([true])没有成功。清空缓存似乎还不够,而且我也不可能要求最终用户这样做。

4

0 回答 0