Saturday, May 19, 2007

Script Debugging Alternative in IE

Lately I have been experiencing problems with the Script debugger in Visual Studio 2005. The debugger is started by inserting a debugger statement in your code like so:



function doSomething()


{

for (i = 0; i < 10; i++)

{


debugger;

document.write(i)

}

}


This method of debugging is great because it allows you to debug just as you would with server side code. There are several blogs on the web that go into detail about how to do this. The problem I am having is that the Visual Studio script debugger randomly hangs for no apparent reason.

The other day I was forced to search for a better solution to solve a particular problem I had. One way that I have done this in the past is to use alert() statements, which is just ok at best, so I started asking around at work to see what others have done. One solution that a co-worker of mine (Ryan Garrett) had used was to use document.write()to output information to a new window like so:


var win;

if (!win)

{

win = window.open("","debug","width=400,height=260");

for (i = 0; i < 10; i++)

{

win.document.write(i);

}

}


This is a simple concept, and helped me tremendously in the problem I was solving. The code above instantiates a new window, iterates through a loop 10 times and displays the value of the index on each pass through the loop. The if(!win) check reuses the same window on each pass through the loop. This is important when you have several values you want to output, it prevent a new window from opening up for every value you display. Below is a screenshot of the popup this code creates.




1 comment:

Anonymous said...

Interesting to know.