Since I tend to be writing a lot of AJAX lately, which usually involves a fairly large Javascript program, it is easy to introduce Javascript errors that aren’t within a try-catch or are only produced in obscure web-browsers. To make sure the public never gets a browser-prompt about an error, I’ve begun writing custom error handlers. In theory, they could log the event or execute some code to deal with the error, however, most of the time I’m happy to just cover it up/display it in an understated way.
The following Javascript will display the line number and error message in an HTML entity (e.g. a DIV) with an id "jserrors". I have it running on my blog now so click here to generate a JS error and look at the very bottom of the page to see the error message. (Doesn’t work in Konqueror)
<script language="javascript">
window.onerror = function( msg, page, line )
{
if( document.getElementById("jserrors") )
{
document.getElementById("jserrors").innerHTML += line + ": " + msg + "<br/>";
}
return true;
}
</script>
Ooh, that I like a whole lot. Good job!
Doesn’t seem to work in Safari either, probably since they’re built off the same core libraries. You really should get a Mac at some point, otherwise it’s going to continue to be the bane of your AJAX existence
Probably Safari and Konquer don’t like the window.onload syntax. I’d bet the window.addEventListener/window.attachEvent functions would work. If someone buys me a Powerbook, I’ll write the code myself