Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sequentially load JS files
//three JS files that need to be loaded one after the other
var libs = [
var injectLibFromStack = function(){
if(libs.length > 0){
//grab the next item on the stack
var nextLib = libs.shift();
var headTag = document.getElementsByTagName('head')[0];
//create a script tag with this library
var scriptTag = document.createElement('script');
scriptTag.src = nextLib;
//when successful, inject the next script
scriptTag.onload = function(e){
console.log("---> loaded: " +;
//append the script tag to the <head></head>
console.log("injecting: " + nextLib);
else return;
//start script injection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment