全球JavaScript异常处理程序(在Chrome中) [英] Global javascript exception handler (in Chrome)

查看:173
本文介绍了全球JavaScript异常处理程序(在Chrome中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript中覆盖全局异常处理程序,以便它成为所有未捕获异常的顶级处理程序?

How do I overwrite the global Exception handler in javascript so that it becomes the top level handler for all uncaught exceptions?

编辑:window.onerror没有工作,代码是:

window.onerror didnt work, code is:

<HTML>
 <HEAD>
<script language='javascript'>
    window.onerror = function (em, url, ln) {
        alert(em + ", " + url + ", " + ln);
        return false;
    }

    function fGo() {
        try
        {
            var a = b; // error here : b not defined
        }
        catch (e)
        {
            throw e;
        }
    }
</script>
 </HEAD>
 <BODY>
    <button onclick='fGo()'>GO</button>
 </BODY>
</HTML>

我正在Chrome上测试。开发者控制台注册未捕获的异常,但window.onerror中的alert()不会出现。

I'm testing on chrome, by the way. Developer console registers the uncaught exception, but the alert() in window.onerror does not appear.

推荐答案

Google Chrome不支持 window.onerror ,显然没有在webkit中实现。

Google Chrome doesn't support window.onerror, apparentlly it's not implemented in webkit.

更新:现在Chrome也支持 window.onerror 。 (我有25个评论意味着早期版本)

Update: now also Chrome supports the window.onerror. (I have version 25 comments imply earlier versions as well)

我所做的是使用curring封装JQuery来创建一个始终执行在JQuery函数中尝试... catch

What I did was to wrap JQuery using curring to create a proxy that always does a try...catch in the JQuery functions.

我在 www.js-analytics.com ,但解决方案仅适用于JQuery脚本。

I use it in www.js-analytics.com, however the solution only holds for JQuery scripts.

这篇关于全球JavaScript异常处理程序(在Chrome中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆