How to Use the onerror event to handle JavaScript errors

Source: Internet
Author: User

 

JavaScript has always been a mysterious thing for me, especially in error handling. Writing JavaScript before Firefox came out is really a headache. If the program runs successfully, I had to carefully check the program and use Alert repeatedly to verify whether each piece of code is running normally, which is time-consuming and laborious. Maybe many people laugh at me. Why don't I need some debugging software or plug-ins? The reason is simple: I didn't know at the time. It is rarely used now, because Firefox is available!

Over the past few days, I have accidentally discovered a JavaScript error handling event. By the way, I have studied its usage and suddenly found that it is more efficient than alert.

Onerror event: When an error occurs in the program, the error event is triggered on the window object. Example 1: simple onerror Application

Onerror event instance Code

Onerror event instance 1
<Script type = "text/javascript">
Window. onerror = function (){
Alert ("Unfortunately, another error occurred ");
}

Phplamp ();
</Script>

Run instance 1: "Unfortunately, an error occurs again ". Because the phplamp () function does not exist in the program.

Note: The onerror event must be before other Javascript programs in this document!

Example 2: Use the parameter of the onerror event to determine the error details. The onerror event has three parameters: First: The description of the error, Second: the URL of the file where the error occurs, and third: the row number of the error.

Code
Onerror event instance 2
<Script type = "text/javascript">
Window. onerror = function (msg, url, line ){
Alert (
"Unfortunately, another error occurred. \ n"
+ "\ N error message:" + msg
+ "\ N file:" + url
+ "\ N error row number:" + line
);
}

Phplamp ();
</Script>

When running instance 2, error details are displayed.

Note: This debugging method is used in IE and Firefox. Opera, chrome test failed. Safari is not tested.

 

We generally pass the operation function to be executed to the onerror event through the function name transfer method (reference method), such as window. onerror = reportError; window. onerror = function () {alert ('error')}, but we may not know that the event is triggered with three default parameters, which are error messages, the url and line number of the error page.

 

Code
<Script type = "text/javascript">
Window. onerror = testError;
Function testError (){
Arglen = arguments. length;
Var errorMsg = "number of parameters:" + arglen + ";
For (var I = 0; I <arglen; I ++ ){
ErrorMsg + = "\ n parameter" + (I + 1) + ":" + arguments [I];
}
Alert (errorMsg );
Window. onerror = null;
Return true;
}
Function test (){
Error
}
Test ()
</Script>

First, bind the testError method to the onerror event, and then trigger an error in the test method. During execution in IE, we find the following prompt:
---------------------------
Microsoft Internet Explorer
---------------------------
Number of parameters: 3
Parameter 1: 'error' is not defined
Parameter 2: file: // E: \ yanwei \ test \ testError.html
Parameter 3:14
---------------------------
OK
---------------------------
When an error occurs, the function testError captures three parameters. By binding a function to an onerror event, you can capture the above three parameters when an error occurs on the page.
The following problems are also found during the test:
1. By adding return true to the end of the function, the system error message (IE) is not displayed when a function error occurs ).
2. If multiple errors occur on the page, capture and process the first error and terminate the subsequent program execution.

3. onerror is executed in IE, FF, and other browsers in the same way, and contains these three 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.