问题
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 this:
<script src="debugger.js"></script>
<script>
// some script with an error in it
</script>
And every time the debugger notices an error it sends a feedback to the server.
回答1:
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.
回答2:
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.
回答3:
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
回答4:
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!
回答5:
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
回答6:
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
来源:https://stackoverflow.com/questions/546990/automatic-feedback-on-javascript-error