Debugging Javascript on a Live site - Fun and Games

Published Thursday, November 02, 2006 8:17 AM

I was discussing debugging of Javascript on one of the ASP.NET Ajax forums recently and thought I'd share how I go about it, as you'd be surprised how many people are unaware of this capability.

Note: This only discusses getting the debugging process going, not other debugging techniques such as using tools like Fiddler and others...

Scenario:

A site is experiencing a Javascript error (ie. some alert box in Internet Explorer) and you need to fix it, or some other script related issue.

The site is live.

Approach:

I want to load the site up in Visual Studio 2005 and use its script debugging capabilities to try and nut out the issue.

Pre Requisites:

In internet explorer, you must have the following options/settings unchecked:

You can uncheck both the DIsable Script Debugging options from the Tools --> Internet Options menu selection, then select the Advanced Tab.

Method:

Load up Visual Studio 2005, create a new Web site. Edit the properties page of the site (right click on the website project in the solution explorer window, and select properties.

Select the 'Start Options' in the left pan, and choose the 'Start URL' option in the right pane.

Enter the URL of the site and/or page you wish to debug.

Start a debugging session by hitting F5 or pressing the play button.

When the browser is open, select the Debug' --> Windows --> Script Explorer option in Visual Studio.

You will then see a window that lists out all the scripts that have been loaded by this page, including the page itself. This window will also include any scripts loaded via a WebResource call.

Now here is the tricky bit (well ok, more tricky....). If you double click on one of the entries in the window, you should see the relevant script in the content editor window. You can then place breakpoints, inspect variables and almost all the debugging goodness that goes with server side debugging. Visual Studio does have some quirks though, in that sometimes double clicking on an entry does not reveal the script. You often need to double click on another entry (usually the page is best), then double click on that entry again, or double click on some other entry, then back on the entry you originally wanted. Basically, you need to do the "double click dance". I told you its tricky, but unfortunately VS.Net does not always play well. The rewards are worth it though. Once your script is displayed, you can use all the debugging features that you are used to, making it much easier to find those errors. Although, sometimes the timing of script loading can get even more tricky.

Happy debugging....

Comments

# Dave said on Wednesday, November 01, 2006 7:44 PM

Too bad you can't select only the domains you want to debug. Some days, it seems like I get prompted to debug everone's site.

# jody said on Wednesday, November 01, 2006 9:16 PM

Thanks for that post... I often have issues debugging due to my project layout (ie: the actual code and the website are two seperate solutions)... thats a great way to leverage other ways of debugging....and might finally clue me in...

# Sonali's blog said on Tuesday, November 14, 2006 2:34 PM

Glav has posted this useful information here

# EuGeNe said on Tuesday, November 21, 2006 9:57 AM

If you don't have VS yet want something useful and simple to debug Javascript from IE ... you may want to have a look at http://www.ieforge.com/CompanionJS which sits on top of Microsoft Script Debugger and will do wonder ;)

# Ben Strackany said on Tuesday, November 28, 2006 12:29 PM

You could also use FireBug in FireFox

http://www.getfirebug.com/

Also, if you're debugging AJAX, don't forget Fiddler to sniff HTTP traffic

http://www.fiddlertool.com/fiddler/

- Ben

# Glav said on Wednesday, November 29, 2006 6:26 AM

Hi Ben,

I didn't know about Firebug, but I'll chec it out. Fiddler I know well and devoted some space to it in the book I worked on, Beginning AJAX in ASP.NET. It contains a debugging chapter that talks about how to use Fiddler. Its an awesome tool.

# Mohan said on Friday, February 02, 2007 1:41 AM

The easiest way to debug javascript in vs2005 is to use the debugger statement..

See my blog entry..

http://mohansmindstorms.spaces.live.com/Blog/cns!69AE1BEA50F1D0E7!233.entry

# Haj Mohamed said on Thursday, July 26, 2007 4:04 AM

Really nice,

Thank you..........

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication