原文: http://stackoverflow.com/questions/19141195/how-can-we-listen-for-errors-that-do-not-trigger-window-onerror

window.onerror is triggered whether it was a syntax or runtime error. This page from quirksmode has lists of what error events it will and will not catch.

Could you please provide a small code example to show how we can listen for such errors? Can we listen for SyntaxError too?

For a small code example to show how we can listen for such errors:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
window.onerror = function (errorMsg, url, lineNumber) {
alert(errorMsg + lineNumber);
// alert("This is a stack trace! Wow! --> %s", error.stack);
};
</script>
</head> <body>
<script type="text/javascript">
//var x=document.getElementById("demo").value; //uncomment and run to see
document.write('careless to close the parentheses?'; // ')' is not given
</script>
</body>
</html>

running this example in your browser will pop up an alert message similar to this:

JavaScript error: SyntaxError: missing ) after argument list on line 26 for page_url

In the above example: window.onerror = function(message, url, linenumber), the arguments are:

  1. message: the error message (DOMString)
  2. url: the URL of the file containing the error (DOMString)
  3. linenumber: the line number where the error occurred (unsigned long)

If you run the same example by putting var x=document.getElementById("demo").value; instead of the code with syntax error(as i have shown in the example), it will also be caught by the window.onerror() function and will show an alert message similar to this:

JavaScript error: TypeError: document.getElementById(...) is null on line 25 for page_url

window.onerror acts something like a global try/catch block, allowing you to gracefully handle(even with server logging) uncaught exceptions you didn’t expect to see:

  • uncaught exceptions

    1. throw "some messages"
    2. call_something_undefined();
    3. cross_origin_iframe.contentWindow.document;, a security exception
  • some more compile error

    1. <script>{</script>
    2. <script>for(;)</script>
    3. <script>"oops</script>
    4. setTimeout("{", 10);, it will attempt to compile the first argument as a script

But two major issues described here nicely:

  1. Unlike a local try/catch block, the window.onerror handler doesn’t have direct access to the exception object, and is executed in the global context rather than locally where the error occurred. That means that developers don’t have access to a call stack, and can’t build a call stack themselves by walking up the chain of a method’s callers.

  2. Browsers go to great lengths to sanitize the data provided to the handler in order to prevent unintentional data leakage from cross-origin scripts. If you host your JavaScript on a CDN (as you ought), you’ll get “Script error.”, “”, and 0 in the above handler. That’s not particularly helpful.

shareedit
answered Oct 2 '13 at 16:03
Sage

12k11629

How can we listen for errors that do not trigger window.onerror?的更多相关文章

  1. Capture and report JavaScript errors with window.onerror

    原文:https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html onerror is a sp ...

  2. A Realistic Evaluation of Memory Hardware Errors and Software System Susceptibility

    http://www.cs.rochester.edu/~kshen/papers/usenix2010-li.pdf Abstract Memory hardware reliability is ...

  3. vconsole h5应用ajax请求抓包

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...

  4. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  5. 未压缩的jQuery

    /*! * jQuery JavaScript Library v3.4.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  6. react实战系列 —— React 中的表单和路由的原理

    其他章节请看: react实战 系列 React 中的表单和路由的原理 React 中的表单是否简单好用,受控组件和非受控是指什么? React 中的路由原理是什么,如何更好的理解 React 应用的 ...

  7. ORA 各种oraclesql错误

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  8. python 常用模块之os、sys、shutil

    目录: 1.os 2.sys 3.shutil 一.os模块 说明:os模块是对操作系统进行调用的接口 os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 os.chdi ...

  9. 敏捷项目开源管理软件ScrumBasic(2)- 多项目支持

    1.加入Project对象模型 public class Project { [Key] [MaxLength(32)] public string ID { get; set; } public s ...

随机推荐

  1. JS 中的事件绑定、事件监听、事件委托是什么?

    在JavaScript的学习中,我们经常会遇到JavaScript的事件机制,例如,事件绑定.事件监听.事件委托(事件代理)等.这些名词是什么意思呢,有什么作用呢? 事件绑定 要想让 JavaScri ...

  2. CAD交互绘制带周长面积的矩形框(网页版)

    主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...

  3. 环球影城母公司:务必阻止复仇者和 X 战警团聚

    今日导读 去年 12 月,迪士尼宣布收购 21 世纪福克斯后,许多漫威粉丝们马上欢呼:复仇者终于可以和 X 战警团聚了!然而,超级英雄大团圆这个美好景象恐怕不会那么容易实现.就在近日,环球影城母公司— ...

  4. Caused by: java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for com.bj186.crm.pojo.User

    原因分析: myBatis在写delete语句的时候,多写了一个属性 错误的代码: <delete id="deleteUser" parameterType="I ...

  5. 日常[系统]:Linux新人报到(吐槽%&%……&¥……%

    昨天换了系统,从win7换到了NOIP必须面对的Linux系统. 不得不说,真的很不适应.原本右上角的三个按钮变到了左上角. 可爱的DEVCPP被无情的抛弃了. 又用不惯guide,只好用文本编辑器写 ...

  6. JSP页面通过c:forEach标签循环遍历List集合

    c:forEach>标签有如下属性: 属性 描述 是否必要 默认值items 要被循环的信息 否 无begin 开始的元素(0=第一个元素,1=第二个元素) 否 0end 最后一个元素(0=第一 ...

  7. 使用jave2将音频wav转换成mp3格式

    最近需要用到语音合成功能,网上查阅了一番,发现可以使用腾讯云的语音合成API来完成这个功能,但是腾讯云的api返回的是wav格式的音频文件,这个格式的文件有些不通用,因此需要转换成mp3格式的文件.  ...

  8. tab bar controller

    下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...

  9. laravel 框架发送邮件

    http://www.jb51.net/article/121647.htm https://www.cnblogs.com/yxhblogs/p/5998463.html http://www.ji ...

  10. css 实践记录

    子绝父相 https://developer.mozilla.org/zh-CN/docs/Web/CSS/position 利用子绝父相来实现一种比较老的居中方式:1.明确宽度:2.定位左边到容器的 ...