How to detect when asynchronously loaded jquery has finished loading?

夙愿已清 提交于 2019-12-19 19:55:04

问题


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). But how can I detect if jquery has finished lodading. Is there an event for this in jquery which fires when the library finished loading?

In theory I could load jquery like this:

<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. (In this case jquery is loaded synchronously.) That's why I'm looking for an event which jquery emits when it's loaded. Is there one?


回答1:


You can try this:

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



回答2:


Have you seen http://headjs.com/?. An example it's:

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


来源:https://stackoverflow.com/questions/22230007/how-to-detect-when-asynchronously-loaded-jquery-has-finished-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!