Automatic feedback on JavaScript error

前端 未结 6 1033
再見小時候
再見小時候 2020-12-23 22:56

Is there a way to get an automatic feedback if an error (even syntax errors) occurs when running a JavaScript on the client side?

I was thinking of something like th

6条回答
  •  鱼传尺愫
    2020-12-23 23:36

    EDIT: I misunderstood your question initially, this should work:

    Also note, this needs to go BEFORE any javascript that might cause errors.

    window.onerror = function(msg,u,l)
    {
        txt ="Error: " + msg + "\n";
        txt+="URL: " + u + "\n";
        txt+="Line: " + l + "\n\n";
        //Insert AJAX call that passes data (txt) to server-side script
        return true;
    };
    

    As for syntax errors, you're out of luck. Javascript simply dies when there's a syntax error. There's no way to handle it at all.

提交回复
热议问题