When we catch a JS error, we usually use Try{}catch (e) {} and then get the error message through E.errormessage, and then report the error. But for onerror events that may be very rare, have we thought about how to report the line number where the error is? If you think about whether this problem is also plagued by this question, whether it is not possible to catch the wrong line in JS? In fact, I encountered the above several problems, read someone wrote a section of JS code suddenly found OnError incident, to say onerror this time is also n long ago know, But the three parameters and their special properties have never been understood. After own research test, has some new understanding and the understanding to the OnError event. When there is no error on the page, the Window.onerror event does not exist, that is, null (nonsense!). No mistake, if OnError appears normal? We generally pass the function name in the way (referenced) the action function to be performed to the OnError event, 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 of the error page, and the error line number. You know this is an event, like the onclick and onmouseover events, but it has parameters. We can test this.
<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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
First the Testerror method is bound to the OnError event, and then an error is triggered in the test method, and we find the following prompt when executing in IE:
---------------------------
Microsoft Internet Explorer
---------------------------
Number of parameters: 3
Parameter 1: ' ERROR ' not defined
Parameter 2:file://e:\yanwei\test\testerror.html
Parameter 3:14
---------------------------
Are you sure
---------------------------
It can be found that the function Testerror captures three parameters when an error occurs. By binding a function to a onerror event, you can capture the above three parameters when a page fails.
Some of the following questions were also found in the test:
1, by adding return true at the end of the function, you can not eject the system error message (IE) when the function fails.
2. If the page has multiple errors, only the first error is caught and processed and then the execution of the following program is terminated.
3, the OnError event does not catch all errors, can only catch the function or inside the function error (?? What does this mean, it's not a joke, like ADASDF;
function Test () {
aaaa
}
ADASDF undefined error can be caught
function Test () {
aaaa
}
It is possible to capture an unspecified error from AAAA, and the system error message will be ejected directly for errors in Functiona test () {} or Function test () dd{} that cannot be caught.
4, onerror in IE and FF browser execution is the same, but also contains these three parameters.