Click to return to the DHTML, HTML     
ondrop Event     onerrorupdate Event     DHTML Events    
Web Workshop  |  DHTML, HTML & CSS

onerror Event


Fires when an error occurs during object loading.

Syntax

Inline HTML<ELEMENT onerror = "handler" ... >All platforms
Event propertyobject.onerror = handlerJScript (compatible with ECMA 262 language specification) only
Named script<SCRIPT FOR = object EVENT = onerror>Internet Explorer only

Event Handler Parameters

When this event is bound to the window object, the following parameters apply. These parameters are required in Microsoft® Visual Basic® Scripting Edition (VBScript).

ParameterDescription
sMsgOptional. Description of the error that occurred.
sUrlOptional. URL of the page on which the error occurred.
sLineOptional. Line number on which the error occurred.

Remarks

BubblesNo
CancelsYes
To invoke Cause one of the following errors to occur:
  • Run-time script error, such as an invalid object reference or security violation.
  • Error while downloading an object, such as an image.
Default action
  • Displays the browser error message when a problem occurs.
  • Executes any error handling routine associated with the event.

To suppress the default Internet Explorer error message for the window event, set the returnValue property of the event object to true or simply return true in JScript®.

The onerror event fires for run-time errors, but not for compilation errors. In addition, error dialog boxes raised by script debuggers are not suppressed by returning true. To turn off script debuggers, disable script debugging in Internet Explorer by clicking Tools, Internet Options, and then Advanced.

Event Object Properties

Although event handlers in the DHTML Object Model do not receive parameters directly, the handler can query the event object for data.

Event Object Properties

returnValueSets or retrieves the return value from the event.
srcElementRetrieves the object that fired the event.
typeRetrieves the event name from the event object.

Example

The following examples use the onerror event to handle run-time script errors and object load errors.

Sample Code

This example specifies an invalid script entry. The script in the text field is evaluated when the Throw Error button is clicked. The onerror event fires because the script is invalid. The error results are inserted at the bottom of the sample page instead of in a dialog box.

<SCRIPT>
window.onerror=fnErrorTrap;
function fnErrorTrap(sMsg,sUrl,sLine){
   oErrorLog.innerHTML="<b>An error was thrown and caught.</b><p>";
   oErrorLog.innerHTML+="Error: " + sMsg + "<br>";
   oErrorLog.innerHTML+="Line: " + sLine + "<br>";
   oErrorLog.innerHTML+="URL: " + sUrl + "<br>";
   return false;
}
function fnThrow(){
   eval(oErrorCode.value);
}
</SCRIPT>
<INPUT TYPE="text" ID=oErrorCode VALUE="someObject.someProperty=true;">
<INPUT TYPE="button" VALUE="Throw Error" onclick="fnThrow()">
<P>
<DIV ID="oErrorLog">
</DIV>

This feature requires Internet Explorer 4.0 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

This example shows how to set the handler for the onerror event in script before an image source is specified. When the invalid source is set on the IMG element, the event fires.

<SCRIPT>
var sImg='<IMG STYLE="display: none;" ID=oStub ALT="Default Text">';
function fnLoadFirst(){
   oContainer.innerHTML=sImg;
   oStub.onerror=fnLoadFail1;
   oStub.src="";
   oStub.style.display="block";
}
function fnLoadFail1(){
   oStub.alt="Image failed to load.";
   return true;
}
</SCRIPT>

<INPUT TYPE=button VALUE="Load First Image" onclick="fnLoadFirst()">
<DIV ID=oContainer></DIV>

This feature requires Internet Explorer 4.0 or later. Click the icon below to install the latest version. Then reload this page to view the sample.
Microsoft Internet Explorer

Applies To

[ Object Name ]
PlatformVersion
Win16:
Win32:
Mac:
Unix:
WinCE:
IMG, OBJECT, STYLE, window

See Also

onerrorupdate



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.