VS.NET JavaScript debugging

Add me to the growing list of people who are surprised and pleased to see the fantastic JavaScript debugging support in VS.NET (I discovered the details through Steve Sharrok's blog). To make it work, clear the 'disable script debugging' in IE, then drop the 'debugger;' statement in your script where you want to break:
 
function OnLookup()
{
debugger;
var xr = new XMLHttpRequest();
 
 
Run the page in the debugger in VS.NET, and voila, it's just like debugging server code. The ability to poke around the DOM data structures with the Watch window is huge. No more 'alert' debugging for me!
 
 

Posted Aug 09 2006, 10:31 AM by fritz-onion
Filed under:

Comments

John wrote re: VS.NET JavaScript debugging
on 08-09-2006 9:22 AM
I use this when I'm testing between the javascript and my c# request handler.

but for straight javascript debugging, the firefox FireBug add-in is easier and faster.
Kevin Dente wrote re: VS.NET JavaScript debugging
on 08-09-2006 10:01 AM
It's pretty decent, but ise it for a while, and you start running into it's frustrating limitations - especially around setting breakpoints.
Fritz Onion wrote re: VS.NET JavaScript debugging
on 08-09-2006 10:42 AM
Thanks for the FireBug tip, John - I've used the DOM inspector and a javascript eval plugin, but hadn't seen that one yet. Looks nice!
Shawn Wildermuth wrote re: VS.NET JavaScript debugging
on 08-09-2006 11:26 AM
I like the JS debugging too, and have found the only reliable way to 'set a breakpoint' is to use the 'debugger' javascript command. E.g.:

function someFunct()
{
debugger;
// some code.
}
Jorn Schou-Rode wrote re: VS.NET JavaScript debugging
on 08-09-2006 11:48 AM
Interesting news indeed, but I think I will stick with FireBug. Besides the full blown JavaScript debugger, this extension contains a very nice interface to explore the DOM tree of a page, even though the code is a mess :)
Richard Churchill wrote re: VS.NET JavaScript debugging
on 08-09-2006 9:30 PM
It may be obvious but worth noting that this only works with IE. My default browser is Firefox, for a moment I was confused about why it wasn't working.
Graeme Foster wrote re: VS.NET JavaScript debugging
on 08-10-2006 2:30 AM
You can also do this by attaching to the IE process from within Visual Studio, and viewing the 'running documents' window.

Pick the document with the javascript in it, and set breakpoints from within VS.
SimoneB wrote re: VS.NET JavaScript debugging
on 08-10-2006 3:28 AM
Hi Fritz, I wrote a post about tips and tools for ASP.NET debugging a while ago. http://dotnetslackers.com/community/blogs/simoneb/archive/2006/07/24/216.aspx
Fritz Onion wrote re: VS.NET JavaScript debugging
on 08-10-2006 4:54 AM
Nice list Simone - thanks!
Ian Goodsell - Dev Emporium wrote Surprise! VS.NET 2005 can debug JavaScript
on 08-10-2006 8:06 AM
I just read this on Fritz Onion's blog, he has instructions on how to configure IE and your code to make...
Mike DiRenzo wrote re: VS.NET JavaScript debugging
on 08-10-2006 8:53 AM
I use a similar technique and it is not just related to ASP.NET. It also realted to any client-side programming language available.

Of course clear the debug flag in IE as you mentioned earlier. Purposelfully inject a bug where you want to begin debugging. For example using javascript:

function foo(){
var a = 1;
fffffffffffffffff
var b = 2;
}

The bug, "fffffffffffffffff" will raise an exception, cause the debugger to run, and allow you to choose which debugger to use. Do not use numbers or function names.

Adar Wesley wrote re: VS.NET JavaScript debugging
on 08-11-2006 4:23 AM
I seam to remember debugging client side JavaScript both in VS2003 and event in Visual InterDev (Visual Studio 6).

All the above menthoned techniques for setting breakpoints worked there too. I actually forgot about using the 'running documents' window to set breakpoints. This was the method that worked best (for me) in Visual InterDev.

A couple more points:

1. When debugging is enabled in IE you have the menu option View->Script Debugger->Break at Next Statement. I find this a very usefull way to break into debug at the right time.

2. When using an AJAX framework like Atlas, it builds its DOM extensions on the window object. If I use a class MyNamespace.MyClass with method DoService I can examine what the framework built on the DOM by querying the DOM in the Immediate window like so:
?window.MyNamespace
shows the subnamespaces that were created and the classes.
?window.MyNamespace.MyClass
shows the methods.
?window.MyNamesapce.MyClass.DoService.toString()
Shows the code generated for the method.

What I am really hurting for is a decent intellisence support for client side Javascript.

Lazy Coder wrote JavaScript VS.Net
on 08-13-2006 4:00 AM
"Add me to the growing list of
people who are surprised and pleased to see the fantastic JavaScript...
Deepak Chawla wrote re: VS.NET JavaScript debugging
on 08-19-2006 1:06 PM
I have used the debugger in VS2003 Enterprise Architect for years now. It came in handy in the following ways to me.
1) Learning technologies like Ajax where I could step through the code and make sense of the data could be sent and received.
2) But the benefit lies when you are using third party controls for ASP.NET. You can buy the most famous controls and you would find no documentation of client side rendering and manipulation of the controls on the browser end. This helped me debug why things were not working as expected. Lets put it that way, that I found so many bugs that the one of the largest if not the biggest thrid party .NET control company offered me ( a single developer ) money back :)

Ian wrote re: VS.NET JavaScript debugging
on 09-15-2006 5:16 AM
Try Firebug for Firefox... has breakpoints, and no code changes required :P
Ian wrote re: VS.NET JavaScript debugging
on 09-15-2006 5:17 AM
... but I see someone already mentioned that... D'oh!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?