![](/img/trans.png)
[英]How to check if an asynchronously loaded script has finished loading in javascript
[英]How to detect when asynchronously loaded jquery has finished loading?
我想在頁面上異步加載jquery,然后加載另一個依賴於jquery的腳本(它在加載時執行jquery代碼)。 但是如何檢測jquery是否已經完成了lodading。 在jquery中是否有這樣的事件,當庫完成加載時會觸發?
從理論上講,我可以像這樣加載jquery:
<script async src="jquery.js" onload="jqueryloaded()"></script>
但我希望代碼也適用於舊瀏覽器,這可能不支持async和onload屬性。 (在這種情況下,jquery是同步加載的。)這就是為什么我正在尋找一個jquery在加載時發出的事件。 有嗎?
你可以試試這個:
var script = document.createElement('script');
script.src = 'jquery.js';
script.onload = jqueryloaded; // thx @Cookie_Monster
document.body.appendChild(script);
你見過http://headjs.com/嗎? 一個例子是:
head.load("jQuery.js", function() {
// Call a function when done
console.log("Done loading jQuery");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.