I am rebuilding my lazy bootloader module to accept an asyncronus request, but I have a BIG problem:
Internet Explorer does not support script.onload / onerror !!!
The old script made a global assessment of the target source of the script read using the ajax synchronization call, it works very well, it is a cross browser, and I can make it asynchronously edit 1 variable, but it is very difficult to debug (ALL the source code is executed on one line, and the browser gives not much error information, dividing the code into a line with a regular expression is IMPOSSIBLE because js has blocks with infinite depth, and the regular expression is simply not suitable for this).
This is the code I'm using to create a script (classic):
var script = document.createElement('script');
script.type = 'text/javascript';
script.src =name;
script.name =name;
script.async = true;
script.onload=<my_onload_code>;
script.onerror=<my_onerror_code>;
it will not work on IE because it does not support onload and onerror with a script.
the code below is a fix, but only works if the script is not async
if(script.onreadystatechange!==undefined)
script.onreadystatechange = function() {
if (script.readyState == 'loaded')
<my_onerror_code>;
else
if(script.readyState == 'complete')
<my_onload_code>;
};
I can test it every X milliseconds until the script loads, but this is an ugly solution, and I want to avoid it.
EDIT: this is the code I tried to check every X ms, if the script is loaded, it is not so bad and works better than ajax, the problem is that I can not know if the script was loaded successfully or with an error (onload or onerror )
var script = document.createElement('script');
script.type = 'text/javascript';
script.src =name;
script.name =name;
script.async = true;
script.onload=function(){Lazy_loader.onload(this);};
script.onerror=function(){Lazy_loader.onerror(this);};
if(script.onreadystatechange!==undefined){
script.timer=setInterval(function(){
if (script.readyState == 'loaded' || script.readyState == 'complete')}
if(LOADED???)
Lazy_loader.onload(script);
else
Lazy_loader.onerror(script);
clearInterval(script.timer);
}
},100);
}
document.getElementsByTagName('head')[0].appendChild(script);
I tried using addEventListener / attachEvent functions, but it didn't seem to work (even using addEvent functions from the Internet)
A summary of the parameters is as follows:
- Use AJAX and global eval to load the script (debug hell)
- AJAX eval IE ( , IE)
- AJAX , script ( , "" , )
- script.onreadystatechange( IE) X (UGLY!!!)
- window.onload: AVOID, , , script (. )
- script (AVOID, )
- script.onload IE ( addEventListener/attachEvent?!?)
PLEASE NOTE:I do not want to use window.onload because it only starts when ALL the page loads, I need to run it when only the target script loads (my lazy loading of the script is much more complicated, so please ask why),I I DO NOT WANT to use any third-party library (for example, jquery, prototype, etc.) ,I don’t even want to edit the target script source (for example, when using JSPON or adding a script to notify the script is loading).Hope not too much! Thank.