0

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

<!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>

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

这可能吗?

4

2 回答 2

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 提醒你的事实是 IEaundefined夸克。

于 2013-05-28T20:59:03.780 回答
0

一旦抛出错误,JavaScript 就不会继续执行,也许这个问题会帮助您解决问题

于 2013-05-28T20:58:16.163 回答