Home > Web Front-end > JS Tutorial > New gains from learning about javascript window.onerror event_javascript skills

New gains from learning about javascript window.onerror event_javascript skills

WBOY
Release: 2016-05-16 19:08:13
Original
834 people have browsed it

When catching js errors, we usually use try{}catch(e){}, and then obtain the error information through e.errorMessage and other methods and then report the error. But there may be little concern about the onerror event. Have we thought about how to report the line number where the error is located? If you have ever thought about this, have you ever been troubled by this problem? Do you think it is impossible to capture the wrong line number in js? In fact, I have encountered several of the above problems. Today, I read a piece of js code written by someone and suddenly discovered it. Onerror event, I have known about onerror a long time ago, but I have never understood its three parameters and its special properties. After my own research and testing, I have some new knowledge and 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 there is no error, is it normal if onerror occurs?) We usually pass the operation function to be performed by passing the function name (by reference) Give the onerror event, such as window.onerror=reportError;window.onerror=function(){alert('error')}, but we may not know that the event also has three default parameters when it is triggered. They are error information, The URL of the error page and the error line number. You must know that this is an event, just like onclick and onmouseover events, but it has parameters. We can test it like this.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

First bind the testError method Give the onerror event, and then trigger an error in the test method. When executing in IE, we found the following prompt:
--------------------- ---
Microsoft Internet Explorer
--------------------------
Number of parameters: 3
Parameter 1: 'error' is undefined
Parameter 2: file://E:yanweitesttestError.html
Parameter 3: 14
---------------- -----------
OK
---------------------------
Can be found , when an error occurs, the function testError captures three parameters. By binding the function to the onerror event, you can capture the above three parameters when the page fails.

The following problems were also found during the test:
1. By adding return true at the end of the function, the system error message (IE) will not pop up when the function fails.
2. If multiple errors occur on the page, only the first error will be captured and processed and then the execution of subsequent programs will be terminated.
3. The onerror event cannot capture all errors. It can only capture errors outside or within functions (?? What does this mean? It’s not a joke), such as adasdf;
function test(){
aaaa;
}
Can capture undefined errors of adasdf
function test(){
aaaa;
}
Can capture undefined errors of aaaa, and for functiona Errors in test(){} or function test()dd{} cannot be captured and system error messages will pop up directly. 4. Onerror is executed in the same way in browsers such as IE and FF, and both contain these three parameters.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template