HOW-TO: Debug JavaScript in Internet Explorer

The best tool for debugging JavaScript on Internet Explorer is the Microsoft Script Editor, a free component of Microsoft Office XP/2003. There are other options, but they are less attractive: Microsoft Script Debugger is not very powerful, and Visual Studio .Net is an expensive purchase if all you need is JavaScript debugging for IE.
Microsoft Script Editor is a powerful debugging tool, but getting it to work takes a bit of doing. The following is a quick guide. I’ve also included a slideshow (hosted on slideshare.net) that shows the details of how to debug javascript in internet explorer.

How to debug javascript in IE
View more presentations from Jonathan Boutelle

0) Turn debugging on in IE
Go to tools->internet options->advanced. Make sure that “Disable Script Debugging (other)” and “Disable Script Debugging (Internet Explorer) are NOT checked.
IE_Enable_Javascript.gif
1) Get Microsoft Script Editor
Install Office 2003 or Office XP, if you don’t have it already.
Check that you have it by selecting View->Script Debugger->Open from IE. If you have Script Editor, then you’ll be offered the choice of “New Instance of Microsoft Script Debugger” or “New Instance of Microsoft Script Editor”. Otherwise, you’ll just see “Microsoft Script Debugger”.
new_instance_of_editor.gif
If you see that, then you have Script Editor installed. Congratulations!
2) Using Microsoft Script Editor to debug
In general it is easiest to launch the editor from IE (rather than launching it as a stand-alone application). One handy way to debug is to use IE View->Script Debugger->Break at next statement.
break-at-next-statement.gif
This causes the debugger to activate the next time any JavaScript executes. If you then select “step into code” (F11) the javascript file that contains the code will load automatically.
Alternately, you can put a line like this in your Javascript code.
debugger;
It will create a breakpoint automatically (in Venkmen debugger as well as Script Editor).
When this breakpoint gets hit, your debugger will launch. You will see a message that says “An exception ‘runtime error’ has occurred in script. Possible Debuggers:”.
runtime_error.gif
Don’t fret, nothing has gone wrong. Select Microsoft Script Editor from the list.
Now you’re debugging! Simply step through the code, the way you would in Venkman.
debugger.gif
3) After you’re done
Special care needs to be taken to clean up when you’re done debugging, otherwise IE (and possibly the entire operating system) will hang. I recommend F5 (to continue), removing all breakpoints if that is necessary. You need to make sure that the browser isn’t waiting for the debugger. Then shut down the debugger. When it asks you if you want to stop debugging, say “yes”.
4) Related articles / further reading
An article on IE debugging from Microsoft
Debugging Javascript using visual studio.net
Debugging Javascript using Microsoft Script Debugger
The best JavaScript debugger (for Firefox, naturally) is Venkman.
The ViewRenderedSource Firefox extension makes troubleshooting your DOM tree a snap.
A nice blog article on the Microsoft Script Editor
A great installation guide to Microsoft Script Editor

7 thoughts on “HOW-TO: Debug JavaScript in Internet Explorer

  1. I can’t enable the script debugger as I don’t have a “Script Debugger” menu option on my View menu in IE.
    BTW, I have Office 2003 installed and have unchecked the two checkboxes to enable script debugging and restarted IE.
    Is the Microsoft Script Editor an optional component of Office? Is it possible it wasn’t installed with Office? Or is there another reason why I don’t have this menu option? Thanks for any help.

  2. Richard,
    Entirely possible. I’m one of those people who always installs the whole package.
    Try updating office (Help->Check for updates). Maybe that will work?
    The fact that you don’t see “view->Script Debugger” means that you don’t even have the option of using the lousy Microsoft Script Debugger. You have NO debugging options right now, which must suck.
    Here’s the link to download Script debugger (use IE)
    http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp
    Here’s another article that has good install instructions of Script Editor:
    http://www.mandala.com/javascript/debug_javascript.html

  3. I had to restart IE for the changed settings to take effect, that might be the problem you have Richard.
    My question is: once you hit the breakpoint, what’s the key used to execute just the next line of code? The help is terrible!

  4. when I choose “New Instance of Microsoft Script Editor” from IE6->View->Script Debugger->Open menu, I got a popup warning dialog titled “Microsoft Script Editor” with message “Cannot create Application Data Folder”. Could you tell me why? Thanks.

Comments are closed.