onerror event | error event
Browser support:
Fires when an error occurs while loading an external file.
How to register:
In HTML: <ELEMENT onerror="handler">
In JavaScript:
The
event object is accessible to all event handlers in all browsers.
The properties of the
event object contain additional information about the current event.
To get further details about these properties and the possible event handler registration methods, please see the page for the
event object.
Basic information:
Bubbles
No
Cancelable
Yes
Event object
Event
Actions that invoke the onerror event:
When an error occurs during loading an external file.
Example HTML code 1:
This example illustrates the use of the
onerror event on
image elements:
< head >
< script type = "text/javascript" >
function ErrorHandler () {
alert ( "Cannot load the image!" );
}
</ script >
</ head >
< body >
< img onerror = "ErrorHandler ()" src = "not_existing_image.jpg" />
</ body >
Did you find this example helpful?
yes
no
Example HTML code 2:
This example only works in Internet Explorer. It shows how to detect when an external style file cannot be imported:
< head >
< style onerror = "ErrorHandler ()" >
@import url("notexisting.css") ;
</ style >
< script type = "text/javascript" >
function ErrorHandler () {
alert ( "Cannot load the style file!" );
}
</ script >
</ head >
< body >
</ body >
Did you find this example helpful?
yes
no
Example HTML code 3:
This example only works in Google Chrome and Safari (and in Opera from version 9.5 to 10.5). It shows how to detect when an external JavaScript file cannot be loaded:
< head >
< script type = "text/javascript" >
function ErrorHandler () {
alert ( "Cannot load the JavaScript file!" );
}
</ script >
< script type = "text/javascript" src = "notexisting.js" onerror = "ErrorHandler ()" ></ script >
</ head >
< body >
</ body >
Did you find this example helpful?
yes
no
External links:
Share:
Digg
Del.icio.us
Reddit
Facebook
Twitter
Diigo
User Contributed Comments