Last active
March 16, 2019 16:30
-
-
Save samperrow/811ab1ff9548b38583c886e7dbbed53e to your computer and use it in GitHub Desktop.
This script will load multiple JS files dynamically without jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var loadJS = function() { | |
var index = 0; | |
function createScript(url, location) { | |
var script = document.createElement('script'); | |
script.src = url; | |
script.type = 'text/javascript'; | |
script.location = location; | |
script.onload = indexCount; | |
return script; | |
} | |
var scripts = [ | |
createScript('/path/scriptOne.js', document.head), | |
createScript('/path/scriptTwo.js', document.head), | |
createScript('/path/scriptThree.js', document.body), | |
createScript('/path/scriptFour.js', document.body) | |
]; | |
function sendScript(index) { | |
scripts[index].location.appendChild(scripts[index]); | |
} | |
function indexCount() { | |
if (index < scripts.length) { | |
sendScript(index); | |
index++; | |
} | |
} | |
indexCount(); | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment