I'm trying to split my long JavaScript code into different libraries.
I'm trying to write a wrapper script that will load all my libraries:
//this file loads all the scripts to the page
$(document).ready(function(){
var fileName = getCurrentFileName();
loadScript("scripts/pico/popups.js");
loadScript("scripts/pico/effects.js");
loadScript("scripts/pico/pico.js");
loadScript("scripts/pico/facebook.js");
if (fileName != null)
loadScript("script/pico/" + fileName + ".js");
});
/**
* Load a script to the body element
* @param name the name of script file.js
*/
function loadScript(name){
// Adding the script tag to the body
var body = document.getElementsByTagName('body')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = name;
// Fire the loading
body.appendChild(script);
}
/**
* Get the current HTML file name
* @returns {*} the name of the file or null
*/
function getCurrentFileName(){
try {
var fileName;
fileName = document.location.pathname.match(/[^\/]+$/)[0];
return fileName.split('.')[0];
}
catch (e){
return null;
}
}
but i think facebook.js is loaded before pico.js has finished loading - and these are functions from pico.js inside facebook.js..
how can i make sure the libraries will be loaded in the order i enter it ?