1234567891011121314151617181920212223242526272829303132333435363738 |
- function ScriptLoader() {
- var self = this;
- this.scriptCache = [];
- this.loadingScripts = [];
- this.finishedCallback = function() {};
- this.isAlreadyLoaded = function(scriptPath) {
- return this.scriptCache.indexOf(scriptPath) != -1;
- };
- this.attach = function(scriptPath) {
- if(this.isAlreadyLoaded(scriptPath)) {
- return;
- }
- var domElement = document.createElement("script");
- domElement.src = scriptPath;
- domElement.type="text/javascript";
- domElement.async = true;
- domElement.addEventListener('load', function(event){
- if(self.loadingScripts.indexOf(scriptPath) != -1) {
- self.loadingScripts.splice(self.loadingScripts.indexOf(scriptPath), 1);
- }
- if(self.loadingScripts.length == 0) {
- self.finishedCallback();
- }
- });
- this.loadingScripts.push(scriptPath);
- document.getElementsByTagName("body")[0].appendChild(domElement);
- this.scriptCache.push(scriptPath);
- };
- this.finishedLoading = function(callback) {
- this.finishedCallback = callback;
- }
- };
- var loader = new ScriptLoader();
- var include = function(scriptPath) {
- loader.attach(scriptPath);
- };
|