Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Internet Explorer
Events
 onerror event
onerror event

[This documentation is preliminary and is subject to change.]

Fires when an error occurs during object loading.

Syntax

HTML Attribute <element onerror = "handler(event)">
Event Property object.onerror = handler;
attachEvent Method object.attachEvent("onerror", handler)
addEventListener Method object.addEventListener("error", handler, useCapture)

Standards information

There are no standards that apply here.

Event information

SynchronousNo
BubblesNo
CancelableNo

Event handler parameters

pEvtObj [in]

Type: IHTMLEventObj

Remarks

To suppress the default Windows Internet Explorer error message for the window event, set the returnValue property of the event object to true or simply return true in Microsoft 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 choosing Internet Options from the Tools menu. Click the Advanced tab and select the appropriate check box(es).

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.
  • Windows Internet Explorer 9. An error occurs while fetching media data.

The pEvtObj parameter is required for the following interfaces:

  • HTMLAnchorEvents2
  • HTMLAreaEvents2
  • HTMLButtonElementEvents2
  • HTMLControlElementEvents2
  • HTMLDocumentEvents2
  • HTMLElementEvents2
  • HTMLFormElementEvents2
  • HTMLImgEvents2
  • HTMLFrameSiteEvents2
  • HTMLInputFileElementEvents2
  • HTMLInputImageEvents2
  • HTMLInputTextElementEvents2
  • HTMLLabelEvents2
  • HTMLLinkElementEvents2
  • HTMLMapEvents2
  • HTMLMarqueeElementEvents2
  • HTMLObjectElementEvents2
  • HTMLOptionButtonElementEvents2
  • HTMLScriptEvents2
  • HTMLSelectElementEvents2
  • HTMLStyleElementEvents2
  • HTMLTableEvents2
  • HTMLTextContainerEvents2
  • HTMLWindowEvents2
  • HTMLDocumentEvents4
  • HTMLElementEvents4

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.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onerrorEX.htm

<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.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onerrorEX1.htm

<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
acronym
address
applet
area
audio
b
base
baseFont
bdo
bgSound
big
body
blockQuote
br
button
canvas
caption
center
cite
code
col
colGroup
comment
custom
dd
del
dfn
dir
div
dl
document
dt
em
embed
fieldSet
font
form
frame
frameSet
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
isIndex
kbd
label
legend
li
link
listing
map
marquee
media
menu
meta
nextID
noBR
noFrames
noScript
object
ol
optGroup
option
p
param
plainText
pre
q
rt
ruby
s
samp
script
select
small
source
span
strike
strong
style
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
title
tr
tt
u
ul
var
wbr
video
window
xmp
onerrorupdate

 

 

Build date: 3/14/2012

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
onerror event is not fired if script debugging in Internet Explorer 9 is enabled      Michael Freidgeim   |   Edit   |   Show History
The example page http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onerrorEX.htm works correctly in Chrome (shows Error: Uncaught ReferenceError: someObject is not defined) , but does nothing in IE9 (on Windows7) if script debugging in Internet Explorer is enabled . The same happens if IE9 change to IE7 and IE8 mode.$0 $0 However native IE8 on XP fires onerror regardless if debugging is enabled or disabled.
Tags What's this?: Add a tag
Flag as ContentBug
script.onerror is not supported...      Eric Bréchemier   |   Edit   |   Show History
... as described in a 1999 article by Michael D. Edwards published here:
http://msdn.microsoft.com/en-us/library/ms976144.aspx

In a note at the end of the section entitled "element.onerror: Decentralized Error Handling", Mr Edwards states that "the documentation mistakenly says this works for <SCRIPT> elements too; the error will be fixed in the Workshop documentation for the final release of Internet Explorer 5 in March.". It seems that the error persisted until today.

This issue has been discussed on Stack Overflow, and after all posters failed to produce an example that triggered script.onerror, the relevant article was uncovered in this post:
http://stackoverflow.com/questions/2027849/how-to-trigger-script-onerror-in-internet-explorer/2032014#2032014
Not for external XML      yecril   |   Edit   |   Show History
If a SCRIPT element of type "text/xml" fails to load, its error event handler does not get called.
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker