It's possible if there's a symbol you can test for in each script, or if each script can execute a function call when it's done loading.
Loading the script is easy, as I'm sure you know:
var script = document.createElement('script');
script.src = /* ... the source path ... */;
document.body.appendChild(script);
(You'll see people appending to head instead; doesn't matter, and body is easy to find.)
But the hard part is knowing when it has been downloaded and executed. The only real ways of doing that are by polling to see if a new global symbol defined by the script has been defined, or by having the script actively call you back when it's done loading (a'la JSONP).
Either way, once you've detected the symbol or gotten the callback, you then move on to loading the next script.
Here's a quick-and-dirty sketch of doing it on the basis of finding a global symbol (a property on the window object):
// The scripts to load
var scriptList = {
scripts: [
{
file : 'some-script.js',
callback: function() {
// do some code here
},
symbol : "someSymbol",
timeout : 30000 // We know this one is slow, give it 30 seconds
},
{
file : 'another-script.js',
callback: function() {
// do some code here
},
symbol : "anotherSymbol"
},
// ...
]
};
// Triggering the load
loadScripts(scriptList);
// Library routines to do the load
function loadScripts(list)
{
var index, timeout;
// Start with the first one (loadNextScript starts with an increment)
index = -1;
loadNextScript();
// This function loads the next script in the list; if there are no
// more, it simply returns
function loadNextScript()
{
var script;
// Are there more?
++index;
if (index < list.length)
{
// Yes, append a `script` element
script = document.createElement('script');
script.src = list.file;
document.body.appendChild(script);
// Determine when to time out
timeout = new Date() + (list[index].timeout || 20000); // Time out in Xms, default 20 seconds
// Start polling
setTimeout(pollForScript, 0); // Async, but almost immediately (4-10ms on most browsers)
}
}
// This function polls to see if the current script has loaded yet by
// checking for a global symbol it defines.
function pollForScript()
{
var result;
// Has it been too long?
if (new Date() > timeout)
{
// Yes
result = "timeout";
}
else
{
// Has the symbol been defined?
if (typeof window[list[index].symbol] !== "undefined")
{
// Yes
result = "loaded";
}
else
{
// Nope, keep waiting
setTimeout(pollForScript, 250); // Check every quarter-second
}
}
// Did we get a result?
if (result)
{
// Yes, do the callback telling it of the result
try {
list[index].callback(result);
}
catch (e) {
}
// Load the next script
loadNextScript();
}
}
}