Overview of OnError events in JavaScript and tips for using _javascript

Source: Internet
Author: User
1 , Window.onerror Events

The OnError event is used to assist in processing 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 an error message:

If you do not want the browser to display an error message 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, respectively:

1 message: A string that declares information about the error that occurred (a slight difference in the content of the messages returned in different browsers).

2 URL: A string that declares the URL of the document in which the error occurred.

3 lines: A number that declares the line number of the line of code where the error occurred.

For example:

Ps: This method was tested in safari and opera without passing

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

2 , img Load Error

tag supports onerror handle. The event handle is triggered when an image file is not successfully loaded for a variety of reasons. This method allows you to display the default picture when the current picture fails to load.

For example:

When the network reason or other reason, causes the onerror inside to provide the picture address to be not able to load, the IE will loop executes the OnError event, causes the stack overflow, at this time the page will pop up the alert message box, the error message is: "StackOverflow at line:0".

IE 6.0 typically reports an overflow in a pop-up message box and continues to perform its function. However, there are also some overflow vulnerabilities that will close all open windows without prompting. So when you choose to use this event, you should be cautious!

Note The OnError method for: labels has no additional parameters.

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.