Window. onerror () usage and case analysis _ javascript skills

Source: Internet
Author: User
Currently, when reporting js error information during window. onerror, sort out relevant information. For more information, see Onerror syntax usage

Onerror has three input parameters by default:

• Msg: error message
• Url: The file where the error is located
• Line: the code line of the error, which is an integer.
Window. onerror = function (msg, url, line) {// some code };
ForParameters can be obtained through arguments [0], arguments [1], and arguments [2] in sequence.

In js, we usually use js fault tolerance.

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

Basic Features
You can set returnValue = true or directly return true to prevent the browser from displaying error messages. However, the debugging box popped up by script debuggers is not blocked.
Onerror is triggered only when a running error occurs, and a syntax error is not triggered.

The following three methods can cause onerror:

• Runtime errors, such as invalid object references or security restrictions
• Download error, slice
• Failure to obtain multimedia data in IE9 may also cause
The script tag does not support onerror.

Defined inThe onerror attribute on the tag is equivalent to window. onerror (supported by Firefox, Opera, and no response from IE9 and chrome ).

Browser compatibility

Supported onError by browsers listed in QuirksMode

• Chrome 13 +
• Firefox 6.1 +
• Internet Explorer 5.5 +
• Safari 5.1 +
• Opera 11.61 + (the QuirksMode test is not supported until 11.51, And the 11.61 on hand is supported)
In addition to window objects, onerror elements are supported:

• Full support
• Script IE9/IE10/safari 5.1 +/chrome 13 + support
And Onerror is not supported. </P> <strong> Problem and Solution </strong> </p> <p> for errors in referencing external js files, https://bugs.webkit.org/show_bug.cgi?id=70574 "Href =" https://bugs.webkit.org/show_bug.cgi?id=70574 "> Webkit and https://bugzilla.mozilla.org/show_bug.cgi?id=696301 "Href =" https://bugzilla.mozilla.org/show_bug.cgi?id=696301 "> Mozilla browsers tamper with the original error information, resulting in the following three input parameters obtained by onerror: </p> <p class = "codetitle"> <U> </U> the code is as follows: </p> <p class = "codebody" id = "code57453"> <br/> "Script error. "," ", 0 <br/> </p> <p> for example: http://a.com/index.html , Introduced http://b.com/g.js If g. js fails, the information passed to window. onerror will be tampered. </P> <p> the browser has two features: </p> <p> & amp; #8226; script can execute third-party js files that are not in the same source. <Br/> & #8226; the script element ignores the MIME type of the loaded file and runs it as a script. <Br/> In the attack scenario, the Javascript file of a normal page is introduced to a malicious page, and the js file is automatically executed. if an exception is triggered, an error message may leak some sensitive data. This information will eventually be processed by window. onerror on the malicious page. </P> <p> tested, browsers with this feature (the latest version) include Firefox, Chrome, Safari, and Opera. <Br/> </p> <p> http://www.schemehostport.com/2011/10/x-script-origin-we-hardly-knew-ye.html "Href =" http://www.schemehostport.com/2011/10/x-script-origin-we-hardly-knew-ye.html "> The recommended solution for Adam Barth (work on the security of the Chrome browser at Google) is to use CORS ( http://www.w3.org/TR/cors/ "Href =" http://www.w3.org/TR/cors/ "> Cross-Origin Resource Sharing ). </P> <p> In short, when external js files are introduced in script on the page, a crossorigin (similar to the CROS attribute) is added ). When the server receives the request, it adds an authorization field to the HTTP Header (the value can be a specific domain name): </p> <p> Access-Control-Allow-Origin: * </p> <p> the browser detects that this js has authorized the domain name of this page, so it does not need to tamper with the domain name, and thus the js is passed to the window. onerror error message. </P> <p> after testing, this solution has not been implemented by the browser. <Br/> it is supported in later versions of Chrome and Firefox. </P> other references <p> Internet Explorer http://msdn.microsoft.com/en-us/library/cc197053.aspx "Href =" http://msdn.microsoft.com/en-us/library/cc197053.aspx "> http://msdn.microsoft.com/en-us/library/cc197053.aspx </P> <p> Mozilla Firefox https://developer.mozilla.org/en/DOM/window.onerror "Href =" https://developer.mozilla.org/en/DOM/window.onerror "> https://developer.mozilla.org/en/DOM/window.onerror </P> <p> Opera http://dev.opera.com/articles/view/better-error-handling-with-window-onerror/ "Href =" http://dev.opera.com/articles/view/better-error-handling-with-window-onerror/ "> http://dev.opera.com/articles/view/better-error-handling-with-window-onerror/ </P> <p> Wiki http://www.w3.org/wiki/DOM/window.onerror "Href =" http://www.w3.org/wiki/DOM/window.onerror "> http://www.w3.org/wiki/DOM/window.onerror </P> <p> syntax errors and runtime errors http://www.htmlgoodies.com/primers/jsp/article.php/3610081/Javascript-Basics-Part-11.htm "Href =" http://www.htmlgoodies.com/primers/jsp/article.php/3610081/Javascript-Basics-Part-11.htm "> http://www.htmlgoodies.com/primers/jsp/article.php/3610081/Javascript-Basics-Part-11.htm </P> <p> window. below are some examples for your reference: </p> <p> onerror = function (sMessage, sUrl, sLine ){}; </p> <p> the three parameters of the onerror function are used to determine the exact information of an error, which means: Error information; files with errors; and row numbers with errors. </P> <p> example: </p> <p class = "jb51code"> <pre class = "brush: js;"> 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 <IN Put type = "text" ID = oErrorCode VALUE = "someObject. someProperty = true; "> <input type =" button "VALUE =" Throw Error "onclick =" fnThrow () "> <P> </pre> </p> <p> the above example is worth learning. <Br/> when capturing js errors, we usually use the try {} catch (e) {} method, and then obtain the error information through e. errorMessage and 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? & #63; in fact, I have encountered the above problems, I read a piece of js code written by someone today and suddenly found the onerror event. I want to say that the onerror time is 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. </P> <p class = "jb51code"> <pre class = "brush: js;"> <script type = "text/javascript"> window. onerror = testError; function testError () {arglen = arguments. length; var errorMsg = "number of parameters:" + arglen + ""; for (var I = 0; I </p> <p> first, bind the testError METHOD TO THE onerror event, and then trigger an error in the test method. The following prompt is displayed during execution in IE: <br/> --------------------------- Microsoft Internet Explorer --------------------------- <br/> Number of parameters: 3 <br/> parameter 1: 'error' undefined <br/> parameter 2: file://E:/yanwei/test/testError.html <Br/> parameter <br/> --------------------------- OK --------------------------- <br/> you can find that 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. <Br/> the following problems are also found during the test: <br/> 1. Add return true to the end of the function, the system error message (IE) is not displayed when a function error occurs ). <Br/> 2. If multiple errors occur on the page, capture and process the first error and terminate the subsequent program execution. <Br/> 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;} can capture the error function test () {aaaa;} undefined by adasdf ;} undefined aaaa errors can be captured, but system errors cannot be captured for functiona test () {} or function test () dd. <Br/> 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.