Home >Web Front-end >JS Tutorial >Overview and use of onerror events in JavaScript_javascript skills

Overview and use of onerror events in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:38:441466browse
1, Window.onerrorEvent

The onerror event is used to assist in handling 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 will display the error message:

.

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

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

1) message: a string that declares the error information that occurred (the content of the message returned in different browsers is slightly different).

2) url: A string that declares the URL of the document where the error occurred.

3) line: a number that declares the line number of the line of code where the error occurred.

For example:

Ps: This method failed the test under Safari and Opera

Note: onerror events should be bound before other js programs.

2, imgLoading error

The

tag supports onerror handlers. This event handler is triggered when an image file fails to load due to various reasons. Through this method, the default image can be displayed when the current image fails to load.

For example:

When the image address provided in onerror cannot be loaded due to network reasons or other reasons, IE will execute the onerror event in a loop, causing the stack to overflow. At this time, the alert message box will pop up on the page, and the error message is: "stackoverflow at line:0".

IE 6.0 will generally report the overflow phenomenon with a pop-up message box and continue to perform its functions. However, when some overflow vulnerabilities occur, all open windows will be closed without any prompt. Therefore, you should be careful when choosing to use this event!

Note: The onerror method of the tag has no additional parameters.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn