Window. onerror () usage and instance analysis, and carchive usage instance analysis
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 };
For the form of <body onerror = "some code">, you can obtain parameters in sequence through arguments [0], arguments [1], and arguments [2.
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
<Script> the tag does not support onerror.
The onerror attribute defined on the <body> label 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
<Css> and <iframe> do not support onerror.
Problems and Solutions
For errors in referencing external js files, Webkit and Mozilla browsers tamper with the original error information, resulting in the following three input parameters obtained by onerror:
Copy codeThe Code is as follows:
"Script error.", "", 0
For example.
The reason why the browser does this is to consider two features:
• <Script> attackers can execute third-party js files in different origins.
• The <script> element ignores the MIME type of the loaded file and runs as a script.
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.
Tested, browsers with this feature (the latest version) include Firefox, Chrome, Safari, and Opera.
Adam Barth (work on the security of the Chrome browser at Google) Recommended solution is to use CORS (Cross-Origin Resource Sharing ).
In short, when <script> introduces an external js file in the page, add a crossorigin attribute (similar to the CROS attribute of ). When the server receives 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 that this js has authorized the Domain Name of the page, so it does not need to tamper with the error message sent from this js to window. onerror.
After testing, this solution has not been implemented by the browser.
It is supported in later 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. Below are some examples for your reference:
Onerror = function (sMessage, sUrl, sLine ){};
The three parameters of the onerror function are used to determine the exact information of the error, which means: Error information, file with an error, and row number with an error.
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>
The method in the preceding example is worth learning.
When capturing js errors, we usually use the try {} catch (e) {} method, and then obtain the error information through e. errorMessage and other methods, and then 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? In fact, I encountered the above problems. I read a piece of js code written by someone today and suddenly found the onerror event. I 'd like to say that the onerror time was also known 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.
<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>
First, bind the testError method to the onerror event, and then trigger an error in the test method. During execution in IE, we find the following prompt:
--------------------------- Microsoft Internet Explorer ---------------------------
Number of parameters: 3
Parameter 1: 'error' is not defined
Parameter 2: file: // E:/yanwei/test/testError.html
Parameter 3:14
--------------------------- OK ---------------------------
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.
The following problems are also found during the test:
1. By adding return true to the end of the function, the system error message (IE) is not displayed when a function error occurs ).
2. If multiple errors occur on the page, capture and process the first error and terminate the subsequent program execution.
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.
4. The onerror method is the same in IE, FF, and other browsers, and contains these three parameters.
Articles you may be interested in:
- How to capture and report Js errors with window. onerror
- JS optimized the img label and used onerror to display the default image.
- The onerror event Stack overflow at line: 0 is not found in the img image parsing.
- Onerror events in JavaScript overview and usage
- Js pay attention to the onerror event analysis of img Images
- Use the onerror event of the image to load the default image
- Onerror usage in img labels
- New gains in learning javascript window. onerror events