The Code Page
A series of development tips and code examples by Gabriel McAdams
Feb 02

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.

The problem might have to do with the separation of client and server.  Developers may feel that nothing can be done about client-side errors because they aren’t notified of them as they are when a server-side error occurs.

This is why I have decided to release 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: JSErrorNotifier.zip

This software is subject to the Microsoft Public License (Ms-PL)


Comments

Toi said:    

That's a wonderful tip. Smile

Comment Posted: February 10 2010, 09:19 AM

therapon said:    

I did the same but my solution is ajax/vbnet. When I press the button is not entering in the event JSErrorNotifier1_JavaScriptError. Could be a problem on my setup of the webcontrol?  

Comment Posted: February 12 2010, 03:26 PM

ghmcadams said:    

@therapon: What browser are you using?  I ask because Chrome is not supported yet.  Beyond that, maybe you could show me your code so I can take a look.

Comment Posted: February 13 2010, 01:57 AM

therapon said:    

of course gabriel ... it is a test environment (IE8):
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<%@ Register Assembly="WebControls" Namespace="WebControls" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <input id="Button1" type="button" runat ="server"  value="button" onclick="error_t()"/>
        <cc1:JSErrorNotifier ID="JSErrorNotifier1" runat="server" OnJavaScriptError="JSErrorNotifier1_JavaScriptError">
        </cc1:JSErrorNotifier>
    </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub JSErrorNotifier1_JavaScriptError(ByVal sender As Object, ByVal e As WebControls.JavaScriptErrorEventArgs) Handles JSErrorNotifier1.JavaScriptError
'breakpoint      
dim x =0
    End Sub
End Class

Comment Posted: February 13 2010, 09:12 PM

ghmcadams said:    

@therapon: I ran the code you posted, and it worked fine for me.  I noticed that you placed it in an update panel.  Perhaps if you moved it out directly into the form.  Also, consider placing in your master page, so that all pages benefit.

Comment Posted: February 13 2010, 09:25 PM

therapon said:    

thank you for your answers gabriel. Anyway, so strange... as you suggested I tried to put it in the masterpage ContentPlaceHolder
but still no entering in the breakpoint at the event JSErrorNotifier1_JavaScriptError (now in the master). I ran the code you posted, and it worked fine for me  have you tried to put a breakpoint with VS2008?

Comment Posted: February 14 2010, 09:24 AM

ghmcadams said:    

Yeah.  My breakpoint hit.  Do me a favor - Go to the contact page and send me your project (zipped).  I will run it here and see what I can find.

Comment Posted: February 14 2010, 09:59 AM

therapon said:    

gabriel you're great, i tried to run this website directly on iis and it's working properly but I cannot catch the javascript error on vs2008 yet. ok no problem thanks again

Comment Posted: February 14 2010, 08:34 PM

ghmcadams said:    

If that's the case, then maybe it is getting to your code, but you don't have your project setup for debugging.  Go to this page for information on setting up your project for debugging:  msdn.microsoft.com/en-us/library/e8z01xdh.aspx

Comment Posted: February 14 2010, 08:46 PM

therapon said:    

mah...on my web.config <compilation debug="true" strict="false" explicit="true">. The problem is the project stops in debugging for client-side (javascript Microsoft JScript runtime error: Object expected) but after (break/continue/ignore) is not entering in vb server-side. Running the project without debugging it's doing the right thing. Maybe shall I change the "exceptions" in the vs-debug menu to not stop debugging on javascript errors?

Comment Posted: February 15 2010, 09:43 AM

ghmcadams said:    

This just occurred to me.  You debug differently than I do.  When I press F5 (start debugging) I have the same problem you do.  You see, when you do that, you're debugging both the client (IE) and the server.  Debugging the client must have an effect on window.onerror

It will work if you do this (to only debug the server):

Instead of hitting F5 (or the play button), right click the page and click "View in browser"

Then click Debug > Attach to Process

Choose the process that looks like this:

"ASP.NET Development Server - Port XXXX"

Now it should hit your breakpoint.

Comment Posted: February 15 2010, 07:58 PM

therapon said:    

One question or improvement: Is it possible to get the source code of the relative error line(s) of the page as seen from the client ("document.documentElement.innerHTML")? Or at least the browser used by the client...  

Comment Posted: February 15 2010, 11:00 PM

ghmcadams said:    

Unfortunately, there is no way that I know of to get that text.  There is no API to read the html or external script files by line number.  You would have to load the file manually and go to that line (and what if you're reading a dynamic page?  You would first have to generate the page and parse it.

Comment Posted: February 16 2010, 04:02 AM

sarah said:    

Hello i was see your site,its so wonderfull,and inspiring me to imporve my site
thx

Comment Posted: July 25 2010, 02:12 PM

Weston Lawwill said:    

While this topic may be very touchy for most individuals, my opinion is that there must be a center or frequent ground that we all can find. I do respect that youve added related and intelligent commentary right here though. Thank you!

Comment Posted: July 28 2010, 08:57 AM

Air force one said:    

Splendid weblog! I saw it at google and I should say that entries are nicely believed of. I might be coming back to determine more posts quickly.

Comment Posted: July 30 2010, 04:12 AM

Zachery Bjorkquist said:    

MMM...Maby considere to put here more c# pic

Comment Posted: August 01 2010, 02:02 PM

aperitivo roma said:    

We have really found a golden mine of info here

Comment Posted: August 02 2010, 06:14 AM

Linwood Groehler said:    

I enjoy this web blog

Comment Posted: August 03 2010, 10:43 AM

gelosia said:    

Interesting blog, not like the others!

Comment Posted: August 03 2010, 02:57 PM

Elly Forsgren said:    

Appreciation for taking some time to discuss c#, I feel strongly all about it and love learning additional on this subject. If probable, since you gain expertise, would you�d mind updating your internet site with added information? It is extremely useful 4 me.

Comment Posted: August 05 2010, 09:07 PM

Nike Shox NZ said:    

Very good site, excellent content, I will recommend to my group of readers in college too, I will leave an input that the key to success is to choose a product that's in demand.

Comment Posted: August 06 2010, 08:22 AM

festa di laurea roma said:    

This is a really good blog

Comment Posted: August 07 2010, 05:57 PM

bags said:    

Several Correct Opinions and Mistakes of http://www.bagsok.com What is Bad for the Polish of http://www.bagsok.com/collection/124 ?

Comment Posted: August 16 2010, 09:40 AM

aperitivo roma said:    

We do have to read more blogs like this one for sure !!

Comment Posted: August 16 2010, 09:13 PM

Doctor Glattman said:    

Love your webpage I'm going to subscribe

Comment Posted: August 18 2010, 12:58 AM

Kirk Hercman said:    

You need to in fact moderate the comments here

Comment Posted: August 18 2010, 12:58 AM

Leandro Cocker said:    

MMM...Maby considere to put here more c# pic

Comment Posted: August 18 2010, 07:16 AM

computer repair bronxville said:    

I really feel that this blog will keep my attention for more days.

Comment Posted: August 18 2010, 02:14 PM

critique essay said:    

That is a very impressive post you have posted there.

Comment Posted: August 20 2010, 02:29 AM

posizionamento su motori di ricerca said:    

I have to congratulate you for the good content written here

Comment Posted: August 23 2010, 01:43 PM

Jordan 1 said:    

Exceptionally pleased to get this webpage.I desired to thank you for this fantastic go through! I undoubtedly enjoying every single tiny bit of it and I've you bookmarked to have a look at new things you publish.

Comment Posted: August 27 2010, 02:23 AM

swefew said:    

Comment Posted: August 27 2010, 03:35 AM

bags said:    

http://www.bagsok.com/c-business-laptop , the Best Picks for Sister This Year ! The Designer of http://www.bagsok.com Changed

Comment Posted: August 27 2010, 04:18 AM

north idaho roofer said:    

Comment Posted: August 29 2010, 03:35 PM

how to make coffee said:    

I adore that website layout . How do you make it. It's really cool.

Comment Posted: September 03 2010, 12:04 AM
Add comment








biuquote
  • Comment
  • Preview


Loading



.