yesterday I saw that there are not many examples of js autoloader, and most of them are written by using jquery,so I wrote the one with plain js and loading it's loading libraries synchronously.
Also, here is the link to github repo : simple-js-libraries-autoloader
/**
*
* JavaScript libraries autoloader
* @author Aleksandr Cerkasov <coldworld333@gmail.com>
*
*/
// writing the window.onload scope, because the libraries should be added when
// document is fully loaded
window.onload = function () {
// defining libraries to load
var jsScripts = [
'components/ajaxRequest.js',
'config/main.js',
'config/router.js'
];
// getting <head> element to append the libraries to
var htmlHead = document.getElementsByTagName('head')[0];
// scriptElement is array because we need the library to be loaded maybe even later
var scriptElement = [];
for (var key in jsScripts) {
// creating <script> element
scriptElement[key] = document.createElement('script');
// setting the <script> element's type
scriptElement[key].type = 'text/javascript';
// setting the <script> element's source
scriptElement[key].src = jsScripts[key];
// making it synchronous
scriptElement[key].async = false;
// appending the <script> element to the <head> element
htmlHead.appendChild(scriptElement[key]);
}
}
I hope this will be useful for somebody.