ドキュメント

Events/error

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Events

error( )

Triggers the error event of each matched element.
This causes all of the functions that have been bound to that error event to be executed, and calls the browser's default error action on the matching element(s). This default action can be prevented by returning false from one of the functions bound to the error event.

error( fn )

Binds a function to the error event of each matched element.

(NOTE: Please use the browser's native handling for window.onerror rather than this method; it does not currently work properly.) There is no public standard for the error event. In most browsers, the window object's error event is triggered when a JavaScript error occurs on the page. An image object's error event is triggered when it is set an invalid src attribute - either a non-existent file, or one with corrupt image data.

If the event is thrown by the window object, the event handler will be passed three parameters:

  1. A message describing the event ("varName is not defined", "missing operator in expression", etc.),
  2. the full URL of the document containing the error, and
  3. the line number on which the error occured.

If the event handler returns true, it signifies that the event was handled, and the browser raises no errors.

For more information, see:

Arguments:
fnFunction
An event handler function to bind to the error event.
function callback(eventObject) {
  this; // dom element
}
Examples:

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 });
});

To hide JavaScript errors from the user, you can try:

$(window).error(function(){
  return true;
});

To hide the "broken image" icons for your IE users, you can try:

$("img").error(function(){
  $(this).hide();
});

NameType