Adding Your Own Error Handler

JavaScript FAQ | Error Handling  

Question: Can I set up my own JavaScript error handler?

Answer: Yes. To define your own error handler, use this JavaScript code:

function handlerFunction(description,page,line) {
 // put error-handling operators here
 return true;
}
window.onerror=handlerFunction;
Your error handler function can optionally use the following parameters:
  • a textual description of the error
  • the address (URL) of the page on which the error occurred
  • the number of the line in which the error occurred

The error handler function must return false if you wish to invoke the browser's default error handler after your own handler finishes. If you don't want to invoke the browser's default handler, your handler function must return true. For an additional code example, check out this error handling demo!

See also:

  • Validating a form
  • Disabling the context menu for the right mouse button
  • JavaScript event handling: mouse events
  • JavaScript event handling: keyboard events
  • Copyright © 1999-2011, JavaScripter.net.