admin管理员组文章数量:1302867
I have a lazy loading JavaScript file, how can I catch the event when the class in the file is ready for usage? I only need to load this script in a specific case. Therefor it is not loaded via onload but in a if clause.
The lazy loading code I took from here: /
if (externalClassRequired) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = '.js';
var x = document.getElementsByTagName('script')[0]
x.parentNode.insertBefore(s, x);
// When do I know when the class named "geo" is available?
}
Update:
Sorry guys, I totally forgot about Ajax! :) I was so focused on my problem that I didn't saw the obviously solution by @Tokimon. The simplest solution via jQuery would then be:
$.getScript('.js', function() {
// callback or use the class directly
});
I have a lazy loading JavaScript file, how can I catch the event when the class in the file is ready for usage? I only need to load this script in a specific case. Therefor it is not loaded via onload but in a if clause.
The lazy loading code I took from here: http://friendlybit./js/lazy-loading-asyncronous-javascript/
if (externalClassRequired) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://yourdomain./script.js';
var x = document.getElementsByTagName('script')[0]
x.parentNode.insertBefore(s, x);
// When do I know when the class named "geo" is available?
}
Update:
Sorry guys, I totally forgot about Ajax! :) I was so focused on my problem that I didn't saw the obviously solution by @Tokimon. The simplest solution via jQuery would then be:
$.getScript('http://yourdomain./script.js', function() {
// callback or use the class directly
});
Share
Improve this question
edited Mar 19, 2011 at 14:03
powtac
asked Mar 18, 2011 at 15:48
powtacpowtac
41.1k28 gold badges118 silver badges172 bronze badges
5 Answers
Reset to default 3if (s.readyState) s.onreadystatechange = function () {
if (s.readyState === "loaded" || s.readyState === "plete") {
s.onreadystatechange = null;
callback();
}
}; else s.onload = function () {
callback();
};
Or you could get the script via ajax. Then you are sure that the script is loaded when the ajax succes event is fired :)
If I understand what you want to acplish correctly. This is why callback methods exist.
Explained pretty well here Getting a better understanding of callback functions in JavaScript
As it requires a HTTP request, you should be able to put an onload event on it.
s.onload = function() {
// do something
}
In 'Even Faster Web Sites' Steve Souders offer a solution, which can be seen here.
var onloadCallback = function() {
alert('onload');
}
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.onreadystatechange = function () { // for IE
if (this.readyState == 'plete') onloadCallback();
};
s.onload = onloadCallback; // for other browsers
s.src = 'http://yourdomain./script.js';
var x = document.getElementsByTagName('script')[0]
x.parentNode.insertBefore(s, x);
You could also use an XMLHttpRequest to load the file.
本文标签: JavaScript how to catch event when lazy loaded script is readyStack Overflow
版权声明:本文标题:JavaScript how to catch event when lazy loaded script is ready? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741685656a2392428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论