ホームページ >ウェブフロントエンド >jsチュートリアル >window.onerror()_javascript スキルの使用法と例の分析
onerror syntax uses
onerror has three input parameters by default:
•msg: error message
•url: The file where the error is located
•line: The line of code where the error is located, integer
window.onerror = function(msg, url, line){ // some code };
For the form of 76dc30a2218f22243eb63e771bb54ab1, parameters can be obtained through arguments[0], arguments[1], and arguments[2] in sequence.
What we most commonly use in js is js fault tolerance
window.onerror=function(){return true;}
Basic Features
You can prevent the browser from displaying error messages by setting returnValue=true or returning true directly. But it will not prevent the debugging box from popping up by script debuggers.
Onerror will only be triggered by runtime errors, not by syntax errors.
Onerror can be triggered in the following three ways:
• Runtime errors such as invalid object reference or security restriction
• Download errors such as pictures
•In IE9, failure to obtain multimedia data will also trigger
The 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag does not support onerror.
The onerror attribute defined on the 6c04bd5ca3fcae76e30b72ad730ca86d tag is equivalent to window.onerror (tested, supported by Firefox and Opera, but unresponsive in IE9 and chrome).
Browser Compatibility
Support for onError in each browser listed by QuirksMode
•Chrome 13+
•Firefox 6.1+
•Internet Explorer 5.5+
•Safari 5.1+
•Opera 11.61+ (QuirksMode test 11.51 is not supported yet, the 11.61 I have on hand already supports it)
In addition to the window object, elements that support onerror:
•a1f02c36ba31691bcfe87b2722de723b Full support
•3f1c4e4b6b16bbbd69b2ee476dc4f83a IE9/IE10/safari 5.1+/chrome 13+ supported
3a674e06c111e35179a3a4f5628fe265 and d5ba1642137c3f32f4f4493ae923989c do not support onerror.
Problems and Solutions
For errors in referencing external js files, Webkit and Mozilla browsers will tamper with the original error information, resulting in the last three input parameters obtained by onerror:
例如http://a.com/index.html,引入了http://b.com/g.js,如果g.js出错,最终传递到window.onerror的信息会被篡改。
浏览器之所以做这样的处理,是考虑到两个特性:
•3f1c4e4b6b16bbbd69b2ee476dc4f83a 能执行非同源下的第三方js文件。
•3f1c4e4b6b16bbbd69b2ee476dc4f83a 元素会忽略加载的文件的MIME类型,而当作脚本来执行。
在攻击场景中,恶意页面引入了正常页面的js文件,js文件会自动执行,若发生异常触发的报错信息,可能会泄漏某些敏感数据。这些信息最终会被恶意页面的window.onerror处理。
经测试,存在此特性的浏览器(当前最新版)有Firefox、Chrome、Safari、Opera。
Adam Barth(work on the security of the Chrome browser at Google)建议的解决方案是使用CORS (Cross-Origin Resource Sharing)。
简言之,当在页面中 3f1c4e4b6b16bbbd69b2ee476dc4f83a 引入外部js文件时,增加一个属性crossorigin(类似于a1f02c36ba31691bcfe87b2722de723b 的CROS属性)。服务器在接受到请求时,在HTTP Header里增加一个授权字段(值可以是具体的某个域名):
Access-Control-Allow-Origin: *
浏览器检测到此js已经授权此页面所在域名,则不用再篡改由此js传递到window.onerror的错误信息了。
经测试,此方案尚未被浏览器实现。
已经在Chrome、Firefox的较新版本中支持。
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.下面是一些实例大家可以参考下:
onerror = function(sMessage,sUrl,sLine){};
onerror函数的三个参数用于确定错误确切的信息,代表的意思依次为:错误信息;发生错误的文件;发生错误的行号。
示例:
<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>
上面示例的方法很值得借鉴。
在捕获js错误时,我们通常使用try{}catch(e){}的方式,然后通过e.errorMessage等方式获取错误信息然后报告错误。但对于onerror事件可能很少问津,我们是否思考过如何报告错误所在的行号?如果想过这个是否也被这个问题所困扰过,是否认为在js里不可能捕获错误的行号呢?其实本人就遇到上述的几个问题,今日读某人写的一段js代码顿然发现了onerror事件,要说onerror这个时间也是n久以前就知道了,但对于其所带有的三个参数和其特殊性质却一直没有去了解过。经过自己的研究测试,对onerror事件有了一些新的认识和了解。在页面没有错误时,window.onerror事件是不存在的,也就是null(废话!没出错如果onerror出现还正常吗?)我们一般通过函数名传递的方式(引用的方式)将要执行的操作函数传递给onerror事件,如window.onerror=reportError;window.onerror=function(){alert('error')},但我们可能不知道该事件触发时还带有三个默认的参数,他们分别是错误信息,错误页面的url和错误行号。要知道这个可是事件,就如onclick和onmouseover等事件一样,但它是有参数。我们可以这样测试。
<script type="text/javascript"> window.onerror=testError; function testError(){ arglen=arguments.length; var errorMsg="参数个数:"+arglen+"个"; for(var i=0;i<arglen;i++){ errorMsg+="/n参数"+(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. When executing in IE, we found the following prompt:
---------------------------- Microsoft Internet Explorer ------------------ -------
Number of parameters: 3
Parameter 1: 'error' undefined
Parameter 2: file://E:/yanwei/test/testError.html
Parameter 3: 14
--------------------------- Sure---------------------- -----
It can be found that the function testError captures three parameters when an error occurs. By binding the function to the onerror event, you can capture the above three parameters when the page fails.
The following problems were also found during the test:
1. By adding return true at the end of the function, the system error message (IE) will not pop up when an error occurs in the function.
2. If there are multiple errors on the page, only the first error will be captured and processed and then the execution of subsequent programs will be terminated.
3. The onerror event cannot capture all errors. It can only capture errors outside the function or within the function (?? What does this mean? It’s not a joke). For example, adasdf; function test(){ aaaa; } can capture adasdf unused errors. The defined error function test(){ aaaa; } can capture aaaa undefined errors, but errors in function test(){} or function test()dd{} cannot be captured and a system error message will pop up directly.
4. Onerror is executed in the same way in browsers such as IE and FF, and both contain these three parameters.