1 // (c) BenoƮt PIN 2006-2015
8 var globalScriptRegistry
;
12 function ScriptRegistry() {
13 this.loadedScripts
= {};
14 this.pendingScripts
= [];
15 this.HEAD
= document
.getElementsByTagName('head')[0];
16 this.isLoading
= false;
19 ScriptRegistry
.prototype.loadScript = function(scriptOb
) {
21 if (typeof(scriptOb
) == 'string')
24 scriptUrl
= scriptOb
.getAttribute('src');
27 if (!this.loadedScripts
[scriptUrl
])
28 this.pendingScripts
.push(['url', scriptUrl
]);
31 this.pendingScripts
.push(['code', scriptOb
]);
33 if(!this.isLoading
&& this.pendingScripts
.length
)
34 this._loadNextScript();
37 ScriptRegistry
.prototype._loadNextScript = function() {
38 var firstScript
= this.pendingScripts
[0];
40 switch (firstScript
[0]) {
42 var script
= document
.createElement( "script" );
43 script
.type
= "text/javascript";
44 script
.src
= firstScript
[1];
45 this.HEAD
.appendChild(script
);
46 this.loadedScripts
[script
.src
] = true;
47 this.isLoading
= true;
48 var thisRegistry
= this;
50 script
.onreadystatechange = function(){
51 if (script
.readyState
== 'complete' || script
.readyState
== 'loaded')
52 thisRegistry
._removeScriptAfterLoad();
55 script
.onload = function(){ thisRegistry
._removeScriptAfterLoad(); };
59 /* jshint ignore:start */
60 eval(firstScript
[1].text
);
61 /* jshint ignore:end */
65 console
.group('Embedded script error');
67 console
.info(firstScript
[1]);
71 this._removeScriptAfterLoad();
76 ScriptRegistry
.prototype._removeScriptAfterLoad = function() {
77 this.pendingScripts
.shift();
78 if(this.pendingScripts
.length
)
79 this._loadNextScript();
81 this.isLoading
= false;
84 globalScriptRegistry
= new ScriptRegistry();