A detailed explanation of how to use Window.onerror in JavaScript

Source: Internet
Author: User


Syntax uses onerror default has three incoming parameters:

msg: Error message
URL: File where the error resides
line: Wrong line of code, integral type
123 Window.onerror = function (msg, URL, line) {//some code};
For <body onerror= "some code" > form, the parameters can be obtained sequentially by Arguments[0, Arguments[1, arguments[2].

Basic characteristics
You can prevent the browser from displaying an error message by setting returnvalue=true, or by directly return true. It does not prevent the script debuggers from ejecting the Debug box.

Only run errors trigger onerror, and syntax errors do not trigger.

OnError can be raised in the following three ways:

• Run-time errors, such as invalid object references or security restrictions
• Download errors, such as pictures
• In IE9, the failure to get multimedia data can also cause
<script> label does not support OnError.

The onerror attribute defined on the <body> label is equivalent to Window.onerror (tested, Firefox, opera support, IE9, Chrome unresponsive).

Browser compatibility
Quirksmode listed browsers support onerror:

Chrome 13+
Firefox 6.1+
Internet Explorer 5.5+
Safari 5.1+
Opera 11.61+ (Quirksmode test to 11.51 is not supported yet, I have on hand 11.61 supported)
Elements that support onerror in addition to window objects:

Full support
<script> Ie9/ie10/safari 5.1+/chrome 13+ Support
<css> and <iframe> do not support onerror.

Problems and Solutions
For errors that refer to external JS files, WebKit and Mozilla browsers tamper with the original error message, causing the last onerror to get three incoming parameters:

"Script error.", "", 0

Http://www.111cn.net, for example, introduces Http://b.com/g.js, and if g.js goes wrong, the information that is eventually passed to Window.onerror is tampered with.

The reason that browsers do this is to take into account two features:

<script> can perform non homologous third party JS files.
The <script> element is executed as a script by suddenly slightly the MIME type of the file being loaded.
In the attack scene, the malicious page introduced the normal page of the JS file, JS files will be automatically executed, if there is an abnormal trigger error information, may leak some sensitive data. This information will eventually be handled by the window.onerror of the malicious page.

After testing, browsers with this feature (the latest version) have Firefox, Chrome, Safari, Opera.

The solution proposed by Adam Barth (work on the security of the "Chrome browser at Google") is to use the Cors (cross-origin Resource sharing).

In short, add an attribute Crossorigin (similar to the Cros property) when the external JS file is introduced into the page <script>. When the server accepts the request, it adds an authorization field to the HTTP header (the value can be a specific domain name):

Access-control-allow-origin: *

The browser detects this JS has authorized the domain name of this page, you do not have to tamper with this JS passed to window.onerror error message.

After testing, this scheme has not been implemented by the browser.
has been supported in newer versions of Chrome and Firefox.

Error does not trigger a onerror situation
However, if you manually go to try and catch the error, this error does not trigger the onerror if you do not continue to throw.

For example:


Try{sdflkjwef}catch (e) {...//does not continue to throw errors}

This error does not trigger the OnError event

Try{sdflkjwef}catch (e) {... throw e;}

Continue to throw this error to trigger the OnError event


IE under screen error appears

At the end of the onerror callback method, if the return value is True, the Explorer can avoid the presence of an exclamation point in the bottom left corner of the browser, while the error collection continues

Window.onerror = function (smessage,surl,sline) {};

The three parameters of the OnError function are used to determine the exact information of the error, which means the error message, the file where the error occurred, and the line number where the error occurred.

Example:

  code is as follows copy code
<SCRIPT>
Window.onerror=fnerrortrap;
function Fnerrortrap (SMSG, Surl,sline) {
Oerrorlog.innerhtml= "<b>an error was thrown and caught.</b><p>";
oerrorlog.innerhtml+= "Error:" + sMsg + "<br>";
oerrorlog.innerhtml+= "line:" + sline + "<br>";
oerrorlog.innerhtml+= URL: "+ sURL + <br>";
return false;
}
Function Fnthrow () {
eval (oerrorcode.value);
}
</script>
<input type= "text" Id=oerrorcode value= "someobject.someproperty=true;"
<input type= "button" value= "Throw Error" onclick= "Fnthrow ()"
<p>
<div id= "Oerrorlog"
</div>

The example above is well worth learning. 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.

The code is as follows Copy 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>
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.