New gains in learning javascript window. onerror events

Source: Internet
Author: User

When capturing js errors, we usually use the try {} catch (e) {} method, and then obtain the error information through e. errorMessage and other methods, and then report the error. However, the onerror event may be rarely used. Have we considered how to report the row number of the error? If you have thought about whether this problem is also plagued by this problem, do you think it is impossible to capture the wrong row number in js? In fact, I encountered the above problems. I read a piece of js code written by someone today and suddenly found the onerror event. I 'd like to say that the onerror time was also known n long ago, however, I have never understood the three parameters and their special properties. After my own research and testing, I have a new understanding of the onerror event. When there is no error on the page, the window. onerror event does not exist, that is, null (nonsense! If onerror occurs, is it normal ?) 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. You need to know that this event is like an onclick event or an onmouseover event, but it has parameters. We can test it like this. <Script type = "text/javascript"> window. onerror = testError; function testError () {arglen = arguments. length; var errorMsg = "number of parameters:" + arglen + ""; for (var I = 0; I
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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 events cannot capture all errors. They can only capture out-of-function or in-function errors (?? What does this mean? It's not a joke.) such as adasdf;
Function test (){
Aaaa;
}
Undefined errors of adasdf can be captured.
Function test (){
Aaaa;
}
Undefined aaaa errors can be captured, but system errors cannot be captured for functiona test () {} or function test () dd.
4. The onerror method is the same in IE, FF, and other browsers, and contains these three 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.