Onerror events in JavaScript overview and usage

Source: Internet
Author: User

1 , Window. onerror Event

The onerror event is used to help handle JavaScript errors on the page. When an exception occurs on the page, the error event is triggered on the window object. For example:

In this case, the browser displays the error message:

.

If you do not want the browser to Display error messages in the default dialog box, let the onerror method return true.

In addition, the onerror method provides three parameters to determine the exact error information:

1) message: a string that declares the error message (the message content returned in different browsers varies slightly ).

2) url: a string that declares the URL of the document with an error.

3) line: a number that declares the row number of the wrong code line.

For example:

Ps: This method has not passed the test in safari and Opera.

Note: The onerror event should be bound before other js programs.

2,ImgLoading error

labels support onerror handles. This event handle is triggered when an image file fails to be loaded for various reasons. This method can display the default image when loading the current image fails.

For example:

When the image address provided in onerror cannot be loaded due to network or other reasons, ie executes the onerror event cyclically, causing stack overflow. The alert message box is displayed on the page, error message: "stackoverflow at line: 0 ".

IE 6.0 reports overflow in pop-up message boxes and continues to execute its functions. However, some overflow vulnerabilities close all opened windows without any prompts. Therefore, be careful when using this event!

Notes: The onerror method of the label does not have any additional parameters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.