JavaScript处理window.onerror中的所有错误

时间:2013-05-28 20:43:16

标签: javascript onerror

我正在尝试使用以下代码处理页面中的所有错误:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            window.onerror = function (e, url, line) {
                var data = '{"error":"' + e + '","line":"' + line + '","uri":"' + url + '","client":"' + navigator.appName + '","version":"' + navigator.userAgent + '"}';
                var call = "http://myserver.com/sandbox/error.aspx?e=" + data;

                var jse = document.createElement("script");
                var head = document.getElementsByTagName("head")[0];
                jse.setAttribute("type", "text/javascript");
                jse.setAttribute("src", call);
                head.appendChild(jse);
                alert("fail");
                return true;
            }
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var b;
            a = 5; //a is undefined
            b = 5;
            c = 8; //c is undefined
        </script>
    </body>
</html>

这适用于Internet Explorer开发人员工具(F12),我可以看到请求:

http://myserver.com/sandbox/jserror.aspx?e={"error":"'a' is undefined","line":"23","uri":"file:///C:/User/user/Desktop/Data/JSerror/javascript-error.htm","client":"Microsoft Internet Explorer","version":"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; Zune 4.7)"}

HTTP状态为200 OK,数据将传递到服务器。 问题是只有第一个错误得到处理,如果我这样做:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            window.onerror = function (e, url, line) {
                var data = '{"error":"' + e + '","line":"' + line + '","uri":"' + url + '","client":"' + navigator.appName + '","version":"' + navigator.userAgent + '"}';
                var call = "http://myserver.com/sandbox/error.aspx?e=" + data;

                var jse = document.createElement("script");
                var head = document.getElementsByTagName("head")[0];
                jse.setAttribute("type", "text/javascript");
                jse.setAttribute("src", call);
                head.appendChild(jse);
                alert("fail");
                return true;
            }
        </script>
    </head>
    <body>
        <script type="text/javascript">
            var b;
            a = 5; //a is undefined
            b = 5;
         </script>
         <script type="text/javascript">
            c = 8; //c is undefined
        </script>
    </body>
</html>

然后处理第一个脚本标记的第一个错误和第二个脚本标记的第一个错误,我想处理所有错误,而不仅仅是每个脚本标记中的第一个错误。

这可能吗?

2 个答案:

答案 0 :(得分:3)

你做的事情的问题是2折。

首先回答你的问题,你想要处理的任何大错误都会因为它的性质而停止其余的代码运行。因此,如果您希望在处理错误后继续使用代码,则需要在错误处理中定义某种类型的回调函数。

所以你可以这样做:

<script type="text/javascript">
        window.onerror = function (e, url, line) {
            var data = '{"error":"' + e + '","line":"' + line + '","uri":"' + url + '","client":"' + navigator.appName + '","version":"' + navigator.userAgent + '"}';
            var call = "http://myserver.com/sandbox/error.aspx?e=" + data;

            var jse = document.createElement("script");
            var head = document.getElementsByTagName("head")[0];
            jse.setAttribute("type", "text/javascript");
            jse.setAttribute("src", call);
            head.appendChild(jse);
            alert("fail");
            return do_work(false);
        }
</script>

<script type="text/javascript">
function do_work(bool){  
    if(bool == true){
        var b;
        a = 5; //a is undefined
        b = 5;
        c = 8; //c is undefined
   }else{
       //skip to new function since there was an error here!
   }
}
</script>

或者沿着这些方向发展。

第二个是你的具体例子。

 a = 5;
 c = 8;

javascript中的实际上错误。如果在声明变量之前未能使用var它将被添加到全局范围,您可以在另一个脚本中执行此操作:

alert(window.a);  // would alert 5

IE提醒你aundefined的事实是IE的夸克。

答案 1 :(得分:0)

一旦发生错误,JavaScript就不会继续执行,也许this question可以帮助您解决问题

相关问题