DHTML Events
Fires when an error occurs loading the object.
Inline HTML |
<element onerror = "handler" ... > |
All platforms |
Event property |
object.onerror = handler |
ECMA-262 Language Specification |
Named script |
<script FOR=object EVENT=onerror> |
Internet Explorer® only |
The following parameters apply only when this event is bound to the window object. These parameters are required in VBScript.
Parameter |
Description |
msg |
Optional. Displays an error message. For use with the window object only. |
url |
Optional. Provides the URL of the object on which the error occurred. For use with the window object only. |
line |
Optional. Gives the line number in the source of the scripting error. For use with the window object only. |
Bubbles |
No |
Cancels |
Yes |
To invoke |
Cause an error to occur.
- Cause an error while the window is loading, such as an error in scripting or a security problem.
- Cause an error while downloading an object, such as an image.
|
Default action |
- Displays the browser error message when a problem occurs while loading the window.
- Executes any error handling routine the programmer has associated with the event.
|
An error event occurs when an image does not load properly or when an error occurs during script execution. This event is valid for OBJECT elements, BODY elements, and FRAMESET element.
Setting the returnValue property of the event object to true, or simply returning true in JScript, suppresses the default Internet Explorer® error message for the window.
While event handlers in the Document Object Model do not receive parameters directly, the handler can query the event object for data.
Event Object Properties
returnValue |
Sets or retrieves the return value from the event. |
srcElement |
Retrieves the object that fired the event. |
type |
Retrieves the event name from the event object. |
The examples demonstrate how to handle the onerror event on the window object, using parameters, as opposed to other objects where no parameters apply.
The example below incorporates the three arguments that can be passed for this event to catch errors that occur while the window is loading.
Sample Code
<head>
<script language="JavaScript">
function errortrap(msg,url,line) {
alert(msg + "\n" + url + "\nLine: " + line);
return true;
}
// suppresses IE error messaging
onerror=errortrap;
UnknownObject.should_cause_scripting_error;
</script>
</head>
<body onerror="errortrap()">
</body>
Show me
The following example demonstrates how to handle an error that occurs while an object is being downloaded.
<body>
<img src="whatever.gif" onerror=
"alert(event.srcElement.src +
'\n cannot be located.')">
</body>
Show me