Window.onerror () Usage and case analysis _javascript skills

Source: Internet
Author: User

OnError syntax uses

OnError has three incoming parameters by default:

msg: Error message
URL: File where the error resides
line: Wrong line of code, integral type
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].

JS in our most commonly used is JS fault-tolerant

Window.onerror=function () {return true;}

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

Support for OnError by browsers listed in Quirksmode

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:

Copy Code code as follows:

"Script error.", "", 0

Http://a.com/index.html, 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.

Other references

Internet Explorer http://msdn.microsoft.com/en-us/library/cc197053.aspx

Mozilla Firefox Https://developer.mozilla.org/en/DOM/window.onerror

Opera http://dev.opera.com/articles/view/better-error-handling-with-window-onerror/

Wiki Http://www.w3.org/wiki/DOM/window.onerror

Syntax errors and runtime errors http://www.htmlgoodies.com/primers/jsp/article.php/3610081/Javascript-Basics-Part-11.htm

window. Here are some examples you can refer to:

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:

 <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& 
gt; 

The

example above is well worth learning.
When you catch a JS error, we usually use the way Try{}catch (e) {}, then get the error message in 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> 

The

First binds the Testerror method to the OnError event, then triggers an error in the test method, and we find the following prompt when executing in IE:
---------------------------Microsoft Internet Explorer---------------------------
Parameters: 3
Parameter 1: ' Error ' undefined
parameter 2:file://e:/yanwei/test/ testerror.html
Parameter 3:14
---------------------------OK---------------------------
can be found, When an error occurs, the function Testerror captures three parameters. By binding a function to a onerror event, you can capture the above three parameters when a page fails. The
also found the following problems 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 appears with 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, only outside the function or within the function error (?? What does this mean, it's not a joke, like ADASDF; function test () {AAAA;} can capture Adasdf undefined error function test () {aaaa;} can catch an unspecified error from AAAA, while for Functiona test () {} or Function test () dd{} errors can not be captured and will directly eject the system error message.
4, onerror in IE and FF browser execution is the same, and all contain 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.