Most of the time when you are visiting a site and you encounter a JavaScript error it is up to the browser to handle it. While most do an ok job of dealing with the error none are very graceful. In alerting the visitor to the error (as is the case of MSIE and older versions of Netscape Navigator) or recording the error in a console window (Mozilla & Netscape) none do anything to help the visitor get past the error and keep working. With the many different levels of impact an error might have on a page functioning properly handling an error smartly is not something the browser should be expected to do for you either. In this article I will explain how to use the JavaScript window.onerror event to gracefully deal with errors that may arise as people use your scripts.
The window.onerror Event
The window.onerror event is the most widely supported method that you as a site author have at your disposal to tell the browser how an error encountered on a page should be dealt with. Like all other JavaScript events you can define a function or piece of code to be run when the event is triggered. In this article I will cover a variety of ways to deal with an error — from ignoring the error to forcing the visitor to an error page. While I have made suggestions on when each method might be useful, ultimately it is up to you to decide which method is most appropriate for your pages.
Aside from syntax and related errors the onerror event is also useful for dealing with errors encountered when a browser tries to load an image. I have not covered this usage in this article because I have yet to encounter a real world scenario where this is useful. Even so, each example presented here can be easily be adapted to work for an image.
As I mentioned in a previous opinion piece one of the more important features that I'd like to see in all browsers that have JavaScript support of any kind is proper error handling. At the time of this articles writing, neither Opera nor OmniWeb support the window.onerror event as used.
Suppressing Errors
In cases where JavaScript functionality is not essential to the operation of a page it is usually best to suppress errors completely. For instance, in the case of a rollover script gone awry, an error dialog on every rollover does nothing but make you as the author look very unprofessional in the eyes of visitors. Well, it might also render the page unusable and force the visitor to take their business elsewhere.
To suppress all browser error messages we can create a function that does nothing and assign it to trigger when the error event happens. [View Suppression Example]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>simple javascript error</title> <script type="text/javascript"> <!-- // function handleError() { return true; } window.onerror = handleError; //--> </script> </head> <body onload="doesnotexist()"> </body> </html>
Suppressing the error completely is wonderful when the lack of JavaScript doesn't prevent the page from working, but it does little to help a visitor who is on a page that requires your JavaScript to work.