How to dynamically load a script file (the most basic version, there are also several options):
function loadScriptFile(scriptPath, jsFile, callBack) { var scriptTag = document.createElement("script"); //creates a HTML script element scriptTag.language = "JavaScript"; //sets the language attribute scriptTag.type = "text/javascript"; scriptTag.src = scriptPath + jsFile + ".js"; //the source if (callBack) { scriptTag.onload = callback; //when loaded execute call back } var scriptTagParent = document.getElementsByTagName("script")[0]; if (scriptTagParent) { scriptTagParent.parentNode.insertBefore(scriptTag, scriptTagParent); } else { document.body.appendChild(scriptTag); } }
How it works:
Run loadScriptFile("scripts", "math", startProgram) . The first two arguments point to your file and folder. The final argument is a callback function. Once defined, this will be executed after the script tag completes the download and the script is available in the global scope. The script will be dynamically added to your page. If a script element is present on the page, this will be added before (to maintain a good rating). If it will not be added to the body. (this is only visual).
The callback component is the most interesting. Since your script will now be asynchronical , you will need to use a callback to tell your program that the necessary files are loading. This callback is triggered when the script file is loaded, so you won't get a script error.
Just a basic example of what I had in mind in my comment:
This is not the answer to your question, this is an alternative way (I think it’s better to manage). Pure Javascript (using XML)
XML file: language.xml Basic XML structure:
<language> <l1033 name="english" tag="en-US"> <id1000> <![CDATA[ Hello World! ]]> </id1000> </l1033> <l1031 name="german" tag="de-DE"> <id1000> <![CDATA[ Hallo Welt! ]]> </id1000> </l1031> </language>
What I've done:
I created a root element called a language. Inside this are written two language lines called l1033 for English and l1031 for German. Note that the letter precedes the letter code. XML will throw an error when a tag starts with a number. a CDATA used to prevent any problems with special characters.
Now loading will be done using AJAX:
var xmlLoader = new XMLHttpRequest(); xmlLoader.onreadystatechange = trackRequest; //event to track the request, with call back xmlLoader.open("get", "language.xml", true); //set asynchronous to true xmlLoader.send(null); function trackRequest() { if (this.status == 200 && this.readyState == 4) //all is good { globalLanguageFile = this.responseXML; startProgram(); //fictive function that starts your program } }
XML is now loaded. How to load lines from it?
function loadLanguageString(code, id, fallback) { var word = fallback; if (globalLanguageFile.getElementsByTagName("l"+code).length > 0) { if (globalLanguageFile.getElementsByTagName("l"+code).[0].getElementsByTagName("id"+id).length > 0) {
How to call a function:
loadLanguageString(1031, 1000, "Hello World!");