admin管理员组文章数量:1426072
Instead of messing up my HTML file, I'd like to import my external JavaScript files through another JavaScript file, much like @import
in css.
On several websites, including StackOverflow itself, I noticed that appending a script tag to the DOM can solve this issue; however, this is done asynchronuosly, while the order of my files is important - the second file for example may rely on the first file in the list. When, say, loading jQuery first and then loading a dependency (plugin etc.) of it, the dependency might finish loading earlier and will throw errors because jQuery doesn't exist yet.
Therefore, this does not seem to be an option. How can I synchronously load JavaScript files from within another JavaScript file?
Instead of messing up my HTML file, I'd like to import my external JavaScript files through another JavaScript file, much like @import
in css.
On several websites, including StackOverflow itself, I noticed that appending a script tag to the DOM can solve this issue; however, this is done asynchronuosly, while the order of my files is important - the second file for example may rely on the first file in the list. When, say, loading jQuery first and then loading a dependency (plugin etc.) of it, the dependency might finish loading earlier and will throw errors because jQuery doesn't exist yet.
Therefore, this does not seem to be an option. How can I synchronously load JavaScript files from within another JavaScript file?
Share Improve this question edited Jan 28, 2011 at 21:13 pimvdb asked Jan 28, 2011 at 20:36 pimvdbpimvdb 155k80 gold badges311 silver badges356 bronze badges2 Answers
Reset to default 7You cannot synchronously load JS files from within JS.
What you can do however, is implement a loader queue, something like this:
function ScriptLoader(queue) {
this.started = false;
this.queue = queue || [];
this.currentIndex = 0;
var self = this;
this.next = function() {
if(self.currentIndex == self.queue.length) return;
self.load(self.queue[self.currentIndex]);
self.currentIndex++;
};
this.load = function(dest) {
var s = document.createElement('script');
s.src = dest;
document.getElementsByTagName('head')[0].appendChild(s);
s.onload = self.next;
if('onreadystatechange' in s) {
s.onreadystatechange = function () {
if (this.readyState == 'plete') {
self.next();
}
}
}
};
}
ScriptLoader.prototype.start = function() {
if(!this.started) {
this.next();
this.started = true;
}
};
var loader = new ScriptLoader(['https://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js', 'https://ajax.googleapis./ajax/libs/jqueryui/1.8.9/jquery-ui.min.js', 'http://widgets.twimg./j/2/widget.js']);
loader.start();
In the above example, jQuery
is loaded first, then jQuery UI
, then the Twitter JS widget. :)
Look at RequireJS or LABjs for asynchronous loading of scripts. I would remend using one of these libraries instead of rolling your own.
本文标签: domImport JavaScript file from within JavaScript synchronouslyStack Overflow
版权声明:本文标题:dom - Import JavaScript file from within JavaScript synchronously? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745407919a2657333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论