Home >Web Front-end >JS Tutorial >Overview and use of onerror events in JavaScript_javascript skills
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
Thetag 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.