我有一个延迟加载JavaScript文件,如何在文件中的类准备好使用时捕获事件?我只需要在特定情况下加载此脚本.因此它不是通过onload加载的,而是在if子句中加载的.
我从这里得到的延迟加载代码:http://friendlybit.com/js/lazy-loading-asyncronous-javascript/
if (externalClassRequired) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://yourdomain.com/script.js';
var x = document.getElementsByTagName('script')[0]
x.parentNode.insertBefore(s, x);
// When do I know when the class named "geo" is available?
}
Run Code Online (Sandbox Code Playgroud)
更新:
对不起伙计们,我完全忘记了Ajax!:)我非常专注于我的问题,我没有看到@Tokimon明显的解决方案.通过jQuery最简单的解决方案是:
$.getScript('http://yourdomain.com/script.js', function() {
// callback or use the class directly
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1975 次 |
| 最近记录: |