Automatic feedback on JavaScript error

时光毁灭记忆、已成空白 提交于 2019-11-29 21:54:20

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.

One technique is to use Ajax for Javascript error logging

Every javascript error can be trapped in the window.onerror event. We can return true or false so we can choose if the user shall see the normal javascript error dialog. This script will, in the very unlikely event of a javascript error, gather information about the error and send a httprequest to a page which will log the javascript error to the database.

Alex Reitbort

I'm not sure about syntax errors but it is possible to catch js errors using window.onerror event. Searching for "logging errors in javascript" in google gives you a lot of results...

Logging JavaScript Errors To ASP.NET: http://james.newtonking.com/archive/2006/05/02/Logging-JavaScript-Errors-To-ASP.NET.aspx (Unfortunately the link to download in the post is broken).

Logging Client Side JavaScript Errors to the Server: http://www.codeproject.com/KB/ajax/logclientsidejserrors2srv.aspx

JavaScript Error Notifications: http://www.thecodepage.com/post/JavaScript-Error-Notifications.aspx

window.onerror = MyFunction(msg,url,line);

we pop up a window with the error details, browser type (i.e. navigator.userAgent), etc. all in fields marked READONLY and inviting the user to Submit them.

We have a checkbox "Don't show this again" which is stored in a session cookie - so if they keep getting errors than can disable the Popup.

Despite the fact that we thought this was "well cool", we get very few reports - and I'm not convinced that that is because we get close to zero runtime errors!

Probably best to send only one error report per page. If you get an error on a repeating timer, or the AJAX itself recursively calls an error, you can end up filling your log with useless junk.

Also, be prepared to receive unsolvable errors caused by things like:

  • security proxies diddling with your code
  • scripts from blocked sites
  • unimportant browser chrome errors from extensions
  • IE Mobile, the worst browser in the world

The easiest way for me was installing this app from SiteApps Marketplace:

http://siteapps.com/app/log_javascript_errors_with_ga-181

Once you've installed the platform tag, you can install a lot of apps and the one above is very useful for me. I use it in 14 websites that I developed and I could track javascript errors in Google Analytics.

Hope this can help.

Leandro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!