Wednesday, July 25, 2007

Javascript Error Logging

I love the relatively new try/catch availability in Javascript. Here is another way of capturing javascript errors. This isn't new to a lot of you, but I wanted to blog it so I don't forget it. This example just does an alert, but with a little ajax/ahah code you could easily post your error to some kind of log. Oh yeah, look for my ahah framework next month. Anyway, the example. If you comment out the alert code and set the return to true, this will also suppress javascript errors (hopefully letting your scripts continue to execute) as well.


function logError(sMsg, sUrl, sLine) {
alert("Error\n-------------\nMessage:"+ sMsg + "\nURL:" + sUrl + "\nLine:" + sLine);
return false;
}
window.onerror = logError;

No comments: