gpt4 book ai didi

javascript - 如果尚未加载 jQuery 和 jQuery UI,则在 Bookmarklet 中加载它们

转载 作者:行者123 更新时间:2023-11-29 20:07:57 25 4
gpt4 key购买 nike

我正在开发一个小书签,它需要访问 jquery-ui 和 jquery min。当然,关注的是页面可能已经加载了 jQuery,应该避免冲突。

使用位于 http://benalman.com/code/javascript/jquery/jquery.ba-run-code-bookmarklet.js 的 Ben Alman 的代码我已经能够优雅地引入 jQuery 并在 UI 中进行加载以进行加载但似乎存在延迟问题并且 jQuery UI 尚未准备好立即运行...

有没有更好的方法来处理在执行实际代码之前按顺序加载两个脚本?

(function( window, document, jQuery, req_version, callback, callback_orig, parent, script ){

if ( !window[jQuery] || req_version > window[jQuery].fn.jquery ) {
parent = document.documentElement.childNodes[0];
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js';
parent.appendChild(script);

callback_orig = callback;
callback = function($, L) {
'$:nomunge, L:nomunge';
$(script).remove();
callback_orig( $, L );
};
}

if (typeof jQuery.ui == 'undefined'){
parent = document.documentElement.childNodes[0];
scriptui = document.createElement('script');
scriptui.type = 'text/javascript';
scriptui.src = 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.js';
parent.appendChild(scriptui);
alert('Loading your matches...');
}

(function loopy($){
'$:nomunge'; // Used by YUI compressor.

( $ = window[jQuery] ) && req_version <= $.fn.jquery
? callback( parent ? $.noConflict(1) : $, !!parent ) : setTimeout( loopy, 50 );
})();

})( window, document, 'jQuery', '1.3.2',

function($,L) {
'$:nomunge, L:nomunge';

<all the jquery stuff goes here>

Using jQuery UI in a Bookmarklet 也有类似的问题有深入的答案,但我无法将其从 CoffeeMarklet 转换为“标准”js。

最佳答案

在这里查看我的要点 - 这是一个用于加载 jQuery 的书签模板,但您可以指定要在执行前加载的其他脚本和 css。

https://gist.github.com/2897748

你像这样初始化它。

编辑:如果你需要加载依赖,你可以创建一个数组并根据不同的条件将东西插入其中。

var jsDependencies = [];

// This will only load jQuery UI if it does not exist already.
// Of course if you rely on the page's copy, you have to do some
// more advanced things to check for versions and whatnot.
if(!window.jQuery || !window.jQuery.ui){
jsDependencies.push('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js');
}

var MyBookmarklet = MyBookmarklet || new Bookmarklet({
// debug: true, // use debug to bust the cache on your resources
css: ['/my/style.css'],
js: jsDependencies,
// jqpath: '/my/jquery.js', // defaults to google cdn-hosted jquery
ready: function(base) { // use base to expose a public method
base.init = function(){
// doStuff();
}
base.init();
}
});

关于javascript - 如果尚未加载 jQuery 和 jQuery UI,则在 Bookmarklet 中加载它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11315595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com