Jquery event error

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

.error( handler )Returns: jQueryversion deprecated: 1.8, removed: 3.0

Description: Bind an event handler to the «error» JavaScript event.

  • version added: 1.0.error( handler )

    • handler

      A function to execute when the event is triggered.

  • version added: 1.4.3.error( [eventData ], handler )

    • eventData

      An object containing data that will be passed to the event handler.

    • handler

      A function to execute each time the event is triggered.

Note: This API has been removed in jQuery 3.0; please use .on( "error", handler ) instead of .error( handler ) and .trigger( "error" ) instead of .error().

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 element:

1

<img alt="Book" id="book">

The event handler can be bound to the image:

1

2

3

4

5

alert( "Handler for .error() called." )

.attr( "src", "missing.png" );

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.

The event handler must be attached before the browser fires the error event, which is why the example sets the src attribute after attaching the handler. Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and 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. Use window.onerror instead.

Additional Notes:

  • As the .error() method is just a shorthand for .on( "error", handler ), detaching is possible using .off( "error" ).

Example:

To replace all the missing images with another, you can update the src attribute inside the callback passed to .error(). Be sure that the replacement image exists; otherwise the error event will be triggered indefinitely.

1

2

3

4

5

6

// If missing.png is missing, it is replaced by replacement.png

$( this ).attr( "src", "replacement.png" );

.attr( "src", "missing.png" );

Содержание:

  • .error( handler )

    • .error( handler )
    • .error( [eventData ], handler )
  • Обсуждение
  • Примеры

.error( handler )Возвращает: jQueryversion deprecated: 1.8, removed: 3.0

Описание: Устанавливает обработчик ошибки при загрузке элементов (например отсутствие необходимой картинки на сервере).

  • Добавлен в версии: 1.0.error( handler )

    • handler

      A function to execute when the event is triggered.

  • Добавлен в версии: 1.4.3.error( [eventData ], handler )

    • eventData

      An object containing data that will be passed to the event handler.

    • handler

      A function to execute each time the event is triggered.

This method is a shortcut for .on( "error", handler ).

As of jQuery 1.8, the .error() method is deprecated. Use .on( "error", handler ) to attach event handlers to the error event instead.

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 element:

1

<img alt="Book" id="book">

The event handler can be bound to the image:

1

2

3

4

5

alert( "Handler for .error() called." )

.attr( "src", "missing.png" );

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.

The event handler must be attached before the browser fires the error event, which is why the example sets the src attribute after attaching the handler. Also, the error event may not be correctly fired when the page is served locally; error relies on HTTP status codes and 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. Use window.onerror instead.

Дополнительные замечания:

  • As the .error() method is just a shorthand for .on( "error", handler ), detaching is possible using .off( "error" ).

Примеры использования

.error()    **REMOVED**

JQ Home  << 
Events  << 
.error()

Error event handler.

Description

The .error() method is used to bind an event handler to The JavaScript error event.

  • The error event is sent to elements that are referenced by a document and not loaded correctly by the browser.
  • The event handler for the error event must be attached prior to the browser firing of the error event.
  • Use window.onerror if you need to attach an event handler to the window object as browsers fire off the window error event when a script error occurs. The window
    error event has different arguments and return value requirements and should be used in this case.

This method was deprecated in jQuery 1.8 and removed in 3.0 and we are just showing it for completeness.

  • Use .trigger( «error» ) instead of .error().
  • Use .on( «error», handler ) instead of .error( handler ).

Syntax

Signature Description
.error( handler(eventObject) ) Bind an event handler to the error JavaScript event.
.error( [eventData ,] handler(eventObject) ) Bind an event handler to the error JavaScript event, optionally passing an object of data.

Parameters

Parameter Description Type
handler( eventObject ) A function to execute each time the event is triggered. Function
eventData An object of data to pass to the event handler. Anything

Return

A jQuery object.

.error( handler(eventObject) ) Example
 Events  <<  Top

Bind an event handler to the error JavaScript event.

  • This signature is a shortcut for .on('error', handler).

The example below no longer works from jQuery 3.0 onwards and is just shown as an example for people using earlier versions.

In the example below we show a new message under the ‘div1’ element when the image of chicken masala cannot be found. We are pointing to an invalid Url as shown below, so the image will never load and the error
JavaScript event occurs. This then fires off the $('#curry1').error(addText) code.

We then access these parameters in the function via event.data.paramN and use the passed parameters for our appended data.


<img id="curry1"  src="badurl"  alt="a picture of curry" 
     title="Chicken Masala"  width="130"  height="100" />


$(function(){
  $('#curry1').error(addText);
  function addText(event) {
    $('#scrollspan1').append('error 1 **JavaScript event triggered** ');
  }
});

We will show a message here when the error JavaScript event fires.

.error( [eventData ,] handler(eventObject) ) Example
 Events  <<  Top

Bind an event handler to the error JavaScript event, optionally passing a map of data.

  • This signature is a shortcut for .on('error', handler).

The example below no longer works from jQuery 3.0 onwards and is just shown as an example for people using earlier versions.

In the example below we show a new message under the ‘div’ element when the image of chicken masala cannot be found. We are pointing to an invalid Url as shown below, so the image will never load and the error
JavaScript event occurs. This then fires off the $('#curry2').error({ param1: '#scrollspan2', param2: 'error ', param3: '**JavaScript event triggered** ' }, addText2); code.

What we are doing here is passing across the event object to the function addText2(event). The map we specify, in our case { param1: '#scrollspan2', param2: 'error ', param3: '**JavaScript event triggered** ' }
gets tagged onto the event.data property.

We then access these parameters in the function via event.data.paramN and use the passed parameters for our appended data.


<img id="curry2"  src="badurl"  alt="a picture of curry" 
     title="Chicken Masala"  width="130"  height="100" />


$(function(){
  $('#curry2').error({ param1: '#scrollspan2', param2: 'error 2 ', 
               param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append( event.data.param2 + '<code>' + event.data.param3 + '</code>');
  }
});

We will show a message here when the error JavaScript event fires.

Related Tutorials

jQuery Advanced Tutorials — Lesson 1 — Browser & Loading Events

  • Home
  •  » 
  • jQuery
  •  » 
  • jQuery Events

This article describe about jQuery Event error() Method.

  • 2428

jQuery Event error() Method

If the element is not loaded correctly then error event occurs and it encounter an error.

The error event trigger with error() method or specifies a function to run when an error event occurs.

Note: Shortcut of this method is (bind , handler).

Example

<!DOCTYPE html>

<html>

<head>

<script type=»text/javascript» src=»jquery.js»></script>

<script type=»text/javascript»>

    $(document).ready(function () {

        $(«img»).error(function () {

            $(«img»).replaceWith(«<p>Error loading image!</p>»);

        });

        $(«button»).click(function () {

            $(«img»).error();

        });

    });

</script>

</head>

<body>

<img src=»5722750107_21488e5a51_b1.jpg» alt=»kas3h.jpg» width=»284″ height=»213″ />

<br />

<button>Trigger error event for the image</button>

</body>

</html>

Output

error method pic14.jpg

Note: After click on the button output become.

error method pic15.jpg

Trigger the error event

Generate the trigger with error event for selected element.

Syntax

Bind a Function to the error Event

It is define the function that run when the error event occur for the specific selected element.

Syntax

$(selector).error(function)

You may also want to read these related articles Click here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here


Понравилась статья? Поделить с друзьями:
  • Jquery error is not a function
  • Jquery ajax success error
  • Jquery ajax post error ajax
  • Jquery ajax post error 500
  • Jquery ajax json error