Fires when an error occurs during object loading.
Syntax
| HTML Attribute | <element onerror = "handler(event)"> |
|---|---|
| Event Property | object.onerror = handler; |
| addEventListener Method | object.addEventListener("error", handler, useCapture) |
Event information
| Synchronous | No |
|---|---|
| Bubbles | No |
| Cancelable | No |
Event handler parameters
- pEvtObj [in]
-
Type: IHTMLEventObj
Standards information
There are no standards that apply here.
Remarks
Displays the browser error message when a problem occurs and executes any error handling routine associated with the event.
To invoke this event, do one of the following:
- Run-time script error, such as an invalid object reference or security violation.
- Error while downloading an object, such as an image.
- An error occurs while fetching media data.
Examples
The following examples use the onerror event to handle run-time script errors and object load errors.
The following 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><br />";
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()"/>
<br />
<div id="oErrorLog"></div>
The following 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>
See also
- a
- abbr
- address
- area
- audio
- b
- base
- bdo
- body
- blockQuote
- br
- button
- canvas
- caption
- cite
- code
- col
- colGroup
- comment
- custom
- dd
- del
- div
- dl
- document
- dt
- em
- embed
- fieldSet
- form
- head
- hn
- hr
- html
- i
- iframe
- img
- input type=button
- input type=checkbox
- input type=file
- input type=hidden
- input type=image
- input type=password
- input type=radio
- input type=reset
- input type=submit
- input type=text
- ins
- kbd
- label
- legend
- li
- link
- map
- media
- meta
- object
- ol
- optGroup
- option
- p
- param
- pre
- q
- rt
- ruby
- s
- samp
- script
- select
- small
- source
- span
- strong
- style
- sub
- sup
- table
- tBody
- td
- textArea
- tFoot
- th
- tHead
- title
- tr
- u
- ul
- var
- video
- window
- onerrorupdate
Send comments about this topic to Microsoft
Build date: 11/29/2012