EVENT HANDLER:  onError


onError = myJavaScriptCode

Event handler for Image, Window.

The onError event handler executes the specified JavaScript code or function on the occurance of an error event. This is when an image or document causes an error during loading. The distinction must be made between a browser error, when the user types in a non-existant URL, for example, and a JavaScript runtime or syntax error. This event handler will only be triggered by a JavaScript error, not a browser error.

As well as the onError handler triggering a JavaScript function, it can also be set to onError="null" which suppresses the standard JavaScript error dialog boxes. To suppress JavaScript error dialogs when calling a function using onError, the function must return true (example 2 below demonstrates this).

There are two things to bear in mind when using window.onerror. Firstly, this only applies to the window containing window.onerror, not any others, and secondly, window.onerror must be spelt all lower-case and contained within <script> tags; it cannot be defined in HTML (this obviously doesn't apply when using onError with an image tag, as in example 1 below).

The onFocus event handler uses the following Event object properties.

type - this property indicates the type of event.
target - this property indicates the object to which the event was originally sent.

The first example suppresses the normal JavaScript error dialogs if a problem arises when trying to load the specified image, while example 2 does the same, but applied to a window, by using return true in the called function, and displays a customized message instead.

Code:
<IMG NAME="imgFaulty" SRC="dodgy.jpg onError="null">

Code:
<script type="text/javascript" language="JavaScript">

s1 = new String(myForm.myText.value)

window.onerror=myErrorHandler()

function myErrorHandler() {
alert('A customized error message')
return true
}

</script>

<body onload=nonexistantFunc()>


Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information