tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

.error( handler(eventObject) )

Returns: jQuery

Description: Bind an event handler to the "error" JavaScript event.

.error( handler(eventObject) )

version added: 1.0
handler(eventObject)
A function to execute each time the event is triggered.

.error( [ eventData ], handler(eventObject) )

version added: 1.4.3
eventData
A map of data that will be passed to the event handler.
handler(eventObject)
A function to execute each time the event is triggered.

This method is a shortcut for .bind('error', handler).

The error event is sent to elements, such as images, that are referenced by a document and loaded by the browser. It is called if the element was not loaded correctly.

For example, consider a page with a simple image:

<img src="missing.png" alt="Book" id="book" />

The event handler can be bound to the image:

$('#book').error(function() {
  alert('Handler for .error() called.')
});

If the image cannot be loaded (for example, because it is not present at the supplied URL), the alert is displayed:

Handler for .error() called.

This event may not be correctly fired when the page is served locally. Since error relies on normal HTTP status codes, it will generally not be triggered if the URL uses the file: protocol.

Note: A jQuery error event handler should not be attached to the window object. The browser fires the window's error event when a script error occurs. However, the window error event receives different arguments and has different return value requirements than conventional event handlers.

Examples

Example 1

To keep a server-side log of JavaScript errors, you might want to:
$(window).error(function(msg, url, line){
  jQuery.post("js_error_log.php", { msg: msg, url: url, line: line });
});

Example 2

To hide JavaScript errors from the user, you can try:
$(window).error(function(){
  return true;
});

Example 3

To hide the "broken image" icons for your IE users, you can try:
$("img").error(function(){
  $(this).hide();
});
Was this information helpful?
   

Comments