你如何确保当异步加载jQuery是可用 [英] How do you ensure jQuery is available when loaded asynchronously
本文介绍了你如何确保当异步加载jQuery是可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谷歌允许您加载jQuery的异步像这样:
Google allows you to load jQuery asynchronously like so:
<script src="http://www.google.com/jsapi?key=..." type="text/javascript"></script>
<script>
//<![CDATA[
google.load('jquery', '1.6');
//]]>
</script>
<script src="scripta.js" type="text/javascript"></script>
<script src="scriptb.js" type="text/javascript"></script>
在以后的脚本,我怎么保证的jQuery已经加载?我可以使用的setInterval
来检查是否的jQuery
设置,但假设 scriptb.js
取决于 scripta.js
,都被包裹在一个的setInterval
等待jQuery的,和这样的时间间隔将随机执行任何一个第一次。
In later scripts, how do I ensure jQuery has been loaded? I could use setInterval
to check whether jQuery
is set, but suppose scriptb.js
depends on scripta.js
, and both are wrapped in a setInterval
to wait for jQuery, and so the interval will "randomly" execute either one first.
我该如何处理这个问题?
How can I approach this problem?
谢谢!
推荐答案
指定可选设置回调
(文档):
google.load('jquery', '1.6', {"callback":jqueryLoaded});
function jqueryLoaded(){
alert('loaded');
}
这篇关于你如何确保当异步加载jQuery是可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文