I am using the following function to load scripts into a HTML file,
function addScript(src){
var script = document.createElement('script');
var location = "javascripts/";
src =location + src;
script.setAttribute('src', src);
document.body.appendChild(script);
};
addScript('george.js');
addScript('fred.js');
addScript('main.js')
However, it seems to be able to load files which are too big (as in 200 lines). For example, if george.js has a class George, and the file is too long, attempts to create a new George() would fail.
I am convinced that it is a loading size problem because of the following:
1) new George would still fail if I comment out almost everything out from george.js.
2) new George would succeed if I delete any 10 functions from george.js.
3) new George would succeed if I split it up into 2 files
4). new Fred would fail if I add a lot of comments in fred.js.
5)To be clear, george.js can be
function George(){};
and I could call:
var george = new George();
in my file. However, if george.js becomes
function George(){};
/*
(Insert hundreds of lines of comments here)
*/
the code
var george = new George();
will cause
ReferenceError: George is not defined
Why does this function have trouble loading a file of roughly 200 lines? And what can I do to deal with the problem?
Edit:
Attempt to try the window.setTimeout function.
AfterTimeout = function(){
var george = new George();
}
window.setTimeout(AfterTimeout , 3000);
This does indeed seem to work.
I have ultimately made it work by setting script.async = false;.