Keep your scripts in separate files. Then the following should work:
function loadScript(pathToScript, callback) { var head = document.getElementsByTagName("head")[0]; var script = document.createElement("script"); script.type = "text/javascript"; script.src = pathToScript + "?t=" + new Date().getTime();
The loadScript function loads a script that is on "pathToScript", and once it has finished loading the script, it calls the provided callback. You need a callback because loading an external resource is an asynchronous operation. This means that you basically need a โnotificationโ when it has finished loading an external resource. This is basically what the callback does. If you are dependent on data coming from an asynchronous operation, you cannot disable this operation, and then continue working and try to use the data or resource because the data will not have a completed download. That way, any code that depends on the resource you put inside the callback. Why we do this will be clearer later.
var scripts = ["/path/to/first/script.js", "/path/to/second/script.js"]; function load(i) { if (i < scripts.length) { loadScript(scripts[i], function () { load(++i); }); } else {
The scripts array contains an array of loaded scripts. The order is implied by the position of the script in the array. This way, the script that is in scripts[0] will be loaded before the script in scripts[1] .
The load function ensures that you load your scripts in the desired order. We know that the load operation is asynchronous. We also know that after the operation is completed, a callback will be called. Using this information, we can load the second script after the script has finished loading. There is one argument load , which represents the current index of the scripts array. First we set the index i to 0 , which means that we will first load the script at index 0 . Then we look at the value of i . If it is less than the length of the array, this means that we have not completed all the scripts (think of it as a loop). Therefore, if i less than the length, we call the loadScript function with the value scripts[i] . Then in our callback we call our load function again, but this time we increase the current value of i . Therefore, when the script has finished loading, we end up calling load with an index with the extension, and now we can load our next script.
If i is equal to scripts.length , this means that we have loaded all our scripts, and therefore we will go to the else block, and this is where you put the code that you need to run at the end of the scripts loaded.
The last line is the actual load call with argument 0 , which starts the whole process, starting from the path to the first script, which is located at index 0 scripts array.
You can rewrite the load function as IIFE if you want:
(function load(i) { if (i < resources.length) { loadResource(resources[i], function () { load(++i); }); } })(0);
Here you have a self-invoked function that calls itself in a callback. It is functionally equivalent to the less exotic version above.