The error event is fired whenever a resource fails to load.
These events can be handled by registering an event listener in one of the three ways listed below:
- Using the
onerror=""content attribute (i.e., the attribute specified in the HTML markup)
- Assigning a function to the
onerrorproperty in JS
||no?||no?||HTML5 (in many situations)||
A resource failed to load. There are many specific circumstances when this event is fired, e.g. for the frame, iframe, img[note], link, script[note], style elements. See details in the HTML specification.
TODO: split specific cases into their own rows, as necessary.
||no||no||HTML5: Media elements — Event summary||An error occurs while fetching the media data for an
||?||?||XMLHttpRequest Level 2|
The HTML specification also requires the event to fire for some non-Element objects, including workers, EventSource (server-sent events). (TODO: list them in the table above.)
<img> elements, an error event is triggered in the following cases:
- Network error (DNS error, for instance)
Content-TypeHTTP header (doesn't start with "image/")
Content-LengthHTTP header is 0
- HTTP response code is 204 (No content)
Even though this is not standardized, this behavior is consistent across web browsers.
Nokia phones (Symbian OS) running a WebKit browser do not fire any events when the
Content-Length HTTP header is 0. In this case, no event is fired (neither
Prior to Firefox 14, the
error event script could bubble, and the string "Error loading script" could be passed instead of the
Event object to the event handler (see bug 737087).
DOM Level 3 Events specification
The DOM Level 3 Events specification mentions the
error event, but does not define the specific cases when the event should be fired. It is therefore not mentioned in the section above.