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.
Grammatical mistakes, such as missing parentheses or unmatched brackets, are the major causes of syntax errors in JavaScript. For example, when you must use conditional statements to address multiple conditions, you may miss providing the parentheses as required, leading to syntax flaws.
TypeError is one of the most common errors in JavaScript apps. This error is created when some value doesn't turn out to be of a particular expected type. Some of the common cases when it occurs are: Invoking objects that are not methods.
There are three main types of errors that can occur while compiling a JavaScript program: syntax errors, runtime errors, and logical errors.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With