简体   繁体   English

如何检测异步加载的jquery何时完成加载?

[英]How to detect when asynchronously loaded jquery has finished loading?

I want to load jquery asynchronously on a page and then load an other script which depends on jquery (it executes jquery code when loaded). 我想在页面上异步加载jquery,然后加载另一个依赖于jquery的脚本(它在加载时执行jquery代码)。 But how can I detect if jquery has finished lodading. 但是如何检测jquery是否已经完成了lodading。 Is there an event for this in jquery which fires when the library finished loading? 在jquery中是否有这样的事件,当库完成加载时会触发?

In theory I could load jquery like this: 从理论上讲,我可以像这样加载jquery:

<script async src="jquery.js" onload="jqueryloaded()"></script>

but I want the code to work on older browsers too which may not support the async and onload attributes. 但我希望代码也适用于旧浏览器,这可能不支持async和onload属性。 (In this case jquery is loaded synchronously.) That's why I'm looking for an event which jquery emits when it's loaded. (在这种情况下,jquery是同步加载的。)这就是为什么我正在寻找一个jquery在加载时发出的事件。 Is there one? 有吗?

You can try this: 你可以试试这个:

var script = document.createElement('script');
script.src = 'jquery.js';
script.onload = jqueryloaded; // thx @Cookie_Monster
document.body.appendChild(script);

Have you seen http://headjs.com/ ?. 你见过http://headjs.com/吗? An example it's: 一个例子是:

head.load("jQuery.js", function() {
 // Call a function when done
 console.log("Done loading jQuery");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM