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
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 <script> tag does not support onerror. </script>
The onerror attribute defined on the
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:
• Full support
•<script> IE9/IE10/safari 5.1+/chrome 13+ supported<br />
<css> and <iframe> do not support onerror. </script>
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:
"Script error.","", 0
例如http://a.com/index.html,引入了http://b.com/g.js,如果g.js出错,最终传递到window.onerror的信息会被篡改。
浏览器之所以做这样的处理,是考虑到两个特性:
•<script> 能执行非同源下的第三方js文件。<br /> •<script> 元素会忽略加载的文件的MIME类型,而当作脚本来执行。<br /> 在攻击场景中,恶意页面引入了正常页面的js文件,js文件会自动执行,若发生异常触发的报错信息,可能会泄漏某些敏感数据。这些信息最终会被恶意页面的window.onerror处理。</script>
经测试,存在此特性的浏览器(当前最新版)有Firefox、Chrome、Safari、Opera。
Adam Barth(work on the security of the Chrome browser at Google)建议的解决方案是使用CORS (Cross-Origin Resource Sharing)。
简言之,当在页面中 <script> 引入外部js文件时,增加一个属性crossorigin(类似于<img alt="Usage and example analysis of window.onerror()_javascript skills" > 的CROS属性)。服务器在接受到请求时,在HTTP Header里增加一个授权字段(值可以是具体的某个域名):</script>
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 or within functions (?? What does this mean? It’s not a joke). For example, adasdf; function test(){ aaaa; } can capture errors that adasdf has not yet 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.

JSP注释的分类及用法解析JSP注释分为两种:单行注释:以结尾,只能注释单行代码。多行注释:以/*开头,以*/结尾,可以注释多行代码。单行注释示例多行注释示例/**这是一段多行注释*可以注释多行代码*/JSP注释的用法JSP注释可以用来注释JSP代码,使其更易于阅

c语言exit函数怎么用,需要具体代码示例在C语言中,我们常常需要在程序中提前终止程序的执行,或者在某个特定的条件下退出程序。C语言提供了exit()函数来实现这个功能。本文将介绍exit()函数的用法,并提供相应的代码示例。exit()函数是C语言中的标准库函数,它包含在头文件中。它的作用是终止程序的执行,并且可以带一个整型

Python函数介绍:isinstance函数的用法和示例Python是一门功能强大的编程语言,提供了许多内置函数,使得编程变得更加方便和高效。其中一个非常有用的内置函数是isinstance()函数。本文将介绍isinstance函数的用法和示例,并提供具体的代码示例。isinstance()函数用于判断一个对象是否是指定的类或类型的实例。该函数的语法如下

Python函数介绍:abs函数的用法和示例一、abs函数的用法介绍在Python中,abs函数是一个内置函数,用于计算给定数值的绝对值。它可以接受一个数字参数,并返回该数字的绝对值。abs函数的基本语法如下:abs(x)其中,x是要计算绝对值的数值参数,可以是整数或浮点数。二、abs函数的示例下面我们将通过一些具体的示例来展示abs函数的用法:示例1:计算

苹果快捷指令怎么用随着科技的不断发展,手机已经成为了人们生活中不可或缺的一部分。而在众多手机品牌中,苹果手机凭借其稳定的系统和强大的功能一直备受用户的喜爱。其中,苹果快捷指令这一功能更是让用户们的手机使用体验更加便捷和高效。苹果快捷指令是苹果公司为其iOS12及更高版本推出的一项功能,通过创建和执行自定义指令,帮助用户简化手机操作流程,以达到更高效的工作和

windows10常用快捷键可以为我们省去很多的时间,今天给大家介绍一些常用的快捷键用法,非常的方便快捷,下面一起来看看具体的使用方法吧。Win10快捷键用法介绍复制、粘贴和其他常规键盘快捷方式按此键执行此操作Ctrl+X剪切选定项Ctrl+C(或Ctrl+Insert)复制选定项Ctrl+V(或Shift+Insert)粘贴选定项Ctrl+Z撤消操作Alt+Tab在打开的应用之间切换Alt+F4关闭活动项,或者退出活动应用Windows徽标键+L锁定电脑Windows徽标键+D显示和隐藏桌面F

SQL中distinct用法详解在SQL数据库中,我们经常会遇到需要去除重复数据的情况。此时,我们可以使用distinct关键字,它能够帮助我们去除重复数据,使得查询结果更加清晰和准确。distinct的基本使用方法非常简单,只需要在select语句中使用distinct关键字即可。例如,以下是一个普通的select语句:SELECTcolumn_name

Python中的assert语句是一种用于检查程序内部逻辑错误的工具。它用于确保在程序执行过程中的某个点上的条件为真。如果条件为假,那么assert语句会抛出一个AssertionError异常,并终止程序的运行。assert语句的基本语法如下:assertcondition,message其中,condition是一个表达式,它的值必须为True,否则


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
