Over the years, I have encountered a few web developers who ignore client side (JavaScript) errors in their applications. Some have even asked me for ways to suppress client-side errors all together. With the growing amount of client-side code in web applications, this is a big concern.
It may seem to some that JavaScript errors are inconsequential, but this is not the case. They are errors just like any other. Whenever an error occurs, your application is no longer doing what you designed it to do. A lot of developers feel that there is nothing they can do. That is on the client. I have no way of knowing what happens on the client. In this post, I will provide code that allows the developer to receive notifications whenever a JavaScript error occurs on the client.
This code is a server control designed to allow .Net developers to log JavaScript errors on the server the same way they can log server-side errors. The control has a very small footprint. It’s simple to use. And it takes up no visible space on the page.
The control uses window.onerror to capture JavaScript errors and send the error information to the server using the iCallbackEventHandler interface (note: As of this writing, window.onerror doesn’t work in Google Chrome). Whenever a JavaScript error occurs, the control raises an event on the server that can be handled in the page.
To use the control is very easy. Simply add this to your markup:
<cc1:JSErrorNotifier ID="JSErrorNotifier1" runat="server"
OnJavaScriptError="JSErrorNotifier_JavaScriptError">
</cc1:JSErrorNotifier>
and this to your code-file:
protected void JSErrorNotifier_JavaScriptError(object sender, WebControls.JavaScriptErrorEventArgs e)
{
e.DisplayMessage = "A JavaScript error has been received by the page";
}
Download Source Code: JSErrorNotifier.zip