Reputation: 5724
is there any handler to catch an error when including / loading a js file?
I am looping through an array and appending a the scripts into my head. is there any way I can see if the included JS file has an error ( ergo then I could get rid of the include sequence headaches, just looping through an array of js files that didnt load 5 or so times or evenby grabbing the files that gave errors, maybe handling them, figuring out why they broke, then including them again)
var incFile=[
"Grid.EditGrid"
,"Data.AutoSaveStore"
,"Form.FormPanel"
,"ux.Spotlight"
];
//http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
//This can be extended to include css files aswell
//Dynamic Js file loading
loadJsFile = function(filename){
var fileref=document.createElement('script');
fileref.setAttribute("src", "js"+filename);
fileref.setAttribute("language","javascript");
document.getElementsByTagName("head")[0].appendChild(fileref);
};
//The loop to include java files
for (var f=0;f<=incFile.length-1;f++){
var path = "";
var split = incFile[f].split(".");
//Build the relative path
for(var s=0;s<=split.length-1;s++){
path += "/"+split[s];
};
loadJsFile(path+".js");
};
Upvotes: 0
Views: 341
Reputation: 5501
You can use the onerror event on the document object, window object, or wire up the onerror event on the "fileref".
http://msdn.microsoft.com/en-us/library/cc197053(v=vs.85).aspx
In your case you would just need to modify your code to the following:
fileref.setAttribute("language","javascript");
document.getElementsByTagName("head")[0].appendChild(fileref);
fileref.onerror = myerrorhandler;
function myerrorhandler(){
alert("error");
}
Upvotes: 1