You can register an error handler function by assigning a function object to the window's onerror property so that when an error occurs in the JavaScript within the window, that function will be called.
This can be a useful way of trapping and completely inhibiting the display of error messages to the user. When the error handling function is called, it is passed three arguments:
The textual message that explains the error
The URL of the document that contains the error
The line number within that document where the error occurred
The line number is the physical line number within the document, not a line number within a script, so you should be able to open the document in a text editor and go to the indicated line and identify the problem.
The only way to activate this error handling capability is to assign a function to the onerror property. There is no HTML tag attribute mechanism for defining an error handler for a window although you can associate one with the <IMG> tag with an attribute value.
You can return either true or false as a result of calling your handler:
true - Inhibit any further error processing and abort the script.
false - Hand control back to the browser to deal with the error in the normal way.
Deactivate error handling altogether by calling a function that simply returns true. To restore default error handling to the window later, assign a function handler to the property that merely returns a false value as its result without intervening in the error process in any way.
Beware of the return values. Returning true from an error handler inhibits the browser from carrying out any further action. This is exactly opposite to the return value from a form element event handler, which requires that a false value be returned to inhibit any further action by the browser.
This works on version 4 of Netscape Navigator. It is also fully supported by MSIE version 5. Version 4 of MSIE for Macintosh (and possibly other platforms) allows the error to be trapped but does not pass any meaningful argument values. They are all undefined.
This is not supported on the WebTV platform.
There is a problem with this event handler hook in the Netscape 6.0 browser. The values are not passed to the handler correctly although the handler is called.
See also: | Debugging - client-side, Error events, Error handler, JellyScript, onError |
Prev | Home | Next |
Window.ondragdrop | Up | Window.onfocus |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |