原文: 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

最新文章

  1. 辨析relative与absolute
  2. day11---异步IO\数据库\队列\缓存
  3. shutter截图工具
  4. Android PopupWindow 消失后的回掉方法
  5. RPi 2B SD read-only filesytem
  6. node.js在windows下的学习笔记(4)---同步,异步,回调的概念
  7. 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
  8. php 半角与全角相关的正则
  9. [转帖]SD卡&amp;FLASH&amp;USB
  10. 查看Sql语句执行速度
  11. 将USBASP改造成STK502编程器(转)
  12. html标签的嵌套规则分析
  13. Day25 前端自学日记——入坑记
  14. Linux CentOS7.0 (03)安装验证 docker
  15. Simditor 富文本编辑器多选图片上传、视频连接插入
  16. Java 8 Lambda 表达式(二)
  17. selenium使用技巧
  18. ionic 照相机 Camera
  19. python-迭代器、生成器、内置函数及面向过程编程
  20. ora-01017 和oracle database link

热门文章

  1. 指针-AC自动机
  2. manjaro利用docker使用MySQL
  3. Spring-1-IOC
  4. 第1节 flume:6、flume的入门测试案例
  5. struts2中关于jsp页面向action传值出现乱码问题
  6. 将中文库导入到ARM板子中以解决中文显示乱码的教程
  7. 2019年,Python工程师必考的6个面试题,Python面试题No5
  8. POJ 4118 开餐馆
  9. 65.什么是IOC?【从零开始学Spring Boot】
  10. [Istioc]Istio部署sock-shop时rabbitmq出现CrashLoopBackOff