single include script with variables to be use right after

Friday, 29. September 2017

To complement what is needed after the post “Include script files in one script file”, I found that some variables needed to be initialized cannot be done unless the loading of all scripts is completed. Looks like loading all scripts and other script initalizing variable are running in different thread. To solve the problem when error occurs identifying variable to be initialized, all variable initializing functions can be inserted as this:

This is how I altered the “loadScriptInOrder” and inserted the initializing scripts in it. Here is the inorder master.js file:
var scriptMap =["/jquery.js","/script1.js", "/script2.js", "/script3.js"];// list of to be loaded script in an array
var order = 0;
function loadScriptInOrder(){
if(order == scriptMap.length) {
initalize_variable1();initialize_variable2();//do this right after the loading
var JSLink = scriptMap[order];
var JSElement = document.createElement(’script’);
JSElement.src = JSLink;
JSElement.onload = callback;

function callback(){
loadScriptInOrder();//load the script in a recursive manner

This way I will not need to load jquery.js in HTML file and then inlude the master.js file. It will be one once and for all. That’s perfect!

Comments are closed.