Function: loadscripts(aScripts, callWhenFinished) – Loading scripts syncron

Sometimes you need to load some javascripts in a syncron way and then when they have loaded do something. This is exactly what I try to address with this function. It requires jQuery. It will take an array of URLs to the scripts (aScripts) you want to load-and-execute. After it has finished with that it will call an optional function “callWhenFinished”.

The code has XML comments which works with Microsoft Visual Studio 2010.

function loadScripts(aScripts, callWhenFinished) {
/// Requires jQuery. Will load the scripts specified in the Array aScripts one after the
other (syncron). After that has been done it will optional
call the callWhenFinishedFunction
/// An array of script URLs which will be loaded an executed using jQuery.getScript
/// After all scripts has been loaded and executed this function will be called
jQuery.getScript( aScripts[0], function(data, textStatus) {
aScripts = aScripts.splice(1,2);
if (aScripts.length>0) {
loadScripts(aScripts, callWhenFinished)
} else {
if (typeof callWhenFinished!='undefined') {
callWhenFinished.call(this)
}
};
});
}