admin管理员组文章数量:1312774
Using javascript to asynchronously download another javascript file.
I understand that this can be done by inserting a new script tag onto the page with the src
attribute set to the file url.
I also need to run some code when the script is finished downloading. I've been using yepnope for this and they provide "callbacks" that execute when the script has finished downloading and executing.
How is this acplished?
Thanks!
Using javascript to asynchronously download another javascript file.
I understand that this can be done by inserting a new script tag onto the page with the src
attribute set to the file url.
I also need to run some code when the script is finished downloading. I've been using yepnope for this and they provide "callbacks" that execute when the script has finished downloading and executing.
How is this acplished?
Thanks!
Share Improve this question asked Aug 24, 2012 at 19:43 Chris DutrowChris Dutrow 50.4k67 gold badges195 silver badges262 bronze badges3 Answers
Reset to default 4Most JS loaders do this via injecting an <script>
tag to the DOM, and binding its onload
event to your provided function.
yepnope
uses the same approach, and you may simply observe that from its source code. The function injectJs
creates a DOM element using doc.createElement
, sets src
and other needed attributes using setAttribute
, binds the onreadystatechange
& onload
event to the provided callback, and finally inserts the element into the document.
yepnope.injectJs( scriptSource [, callback ] [, elemAttributes ] [, timeout ]);
Straight off their website: You simply run the code you need to in the successful callback like so.
// Example
yepnope.injectJs("jquery.js", function () {
console.log("It is finished loading and I can do whatever I need to here");
}, { charset: "utf-8" }, 5000);
this can be done using jquery, if u want to use it, Jquery.getScript()
checkout the link it gives detail information about it.
版权声明:本文标题:yepnope - How to check if an asynchronously loaded script has finished loading in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741872877a2402296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论