<

Debugging JScript Programs with Visual Studio 2008

Published on
11,825 Points
5,725 Views
1 Endorsement
Last Modified:
DanRollins
I use JScript -- Microsoft's version JavaScript -- for writing short utility programs, scheduled tasks, login scripts, and any number of daily chores.  It's a powerful language, and with modern PCs, the slower performance of an interpreted language is usually not an issue.  I use C++ for production code, but JScript is great for simple tasks.

Recently, I got tired of using...
        WScript.Echo( variable_name );
...as my main debugging tool.  Visual Studio provides a full-featured script debugger, and after working with it on a few tasks, I've been kicking myself for not looking into this sooner.

Note: I'll be talking about JScript (.JS files), but most of this article also applies to VB Script (.VBS files) debugging.

Setting Up for Quick Debugging
For me, the whole point of using JScript is that it's all contained in a simple text file.  I don't want to set up a "Solution" or have piles of reference DLLs or anything like that.  I just want to work with a text file that I can tweak until it works and modify at a whim using Notepad.  So, here is how I set up to debug .JS files:

In Windows Explorer, use the Tools / Folder Options command to make a new "Action" for .JS files:
Setting Folder Option in Windows ExplorerLocate the JS - JScript Script Files file entry under the File Types tab.  Select it and click the Advanced button.  Click New... and set:

        Action: Debug This Script
Application: C:\WINDOWS\System32\WScript.exe //d //x "%1"

The //d //x tell WScript to debug the program and to automatically break at the first statement.

OK the changes and close the Folder Options window.  Viola!  Now you can right-click on any .JS file in Windows Explorer and you have a new Debug This Script option in the menu.

So What's Next?
When you start the debugging process, you are prompted with "Do you want to debug using the selected debugger?"  Click Yes.   Up comes Visual Studio, showing your script stopped at the first executable statement.
Script debugging in the IDE
Now you have all of the features of Visual Studio at your command:  Press F10 to single step.  Hover the mouse over a variable to get the variable's current value, and to see an Intellisense list of object members.Intelligent debugging of ActiveX objectsYou can even modify variable values using the tiny popup hover box.Modify variables -- on the fly!And you can use QuickWatch and the Watch window to do real debugging.
The QuickWatch Window
This is where I get the most value:  Visual Studio shows methods and attributes (member functions and member variables) of ActiveX controls, as well as local program variables and objects that you create yourself.   For native values -- primitive variables declared in your code, you can use the QuickWatch and Watch windows to modify values -- set true to false, or modify a numeric value, etc.)

Scroll down and put a break point in the script, and press F5 to continue execution to that point.  You also have the Immediate Window available -- If it's not visible as a tab at the bottom, use the menu command: Debugging / Windows / Immediate to get to it.  

The Immediate Window lets you type in JScript commands such as:
     sTargetFolder="d:\\SomeDir\\"
...so it provides a way to modify any program variable and even create new variables while you step through the code.

The Immediate WindowNotes:  
The "Restart Program" command is not available.   I just right-click on the .JS file and select "Debug This Script" again... this time selecting the existing instance of Visual Studio when prompted.
VS displays a "Dynamic Read-Only" version of your script file while debugging, so how can you make changes to your code?  What I do is drag the JS file into VS so I have a read/write copy of it available.  Just remember to save the changes before restarting the debugging process.
Summary:
Scripts are real programs, too!  Using the steps described in this article, you should be able to develop your scripts faster and and be more productive sooner.  I particularly like the Intellisense quick lists of ActiveX member functions -- it's way easier than than digging through the documentation.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If you liked this article and want to see more from this author,  please click the Yes button near the:
      Was this article helpful?
label that is just below and to the right of this text.   Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
1
Comment
Author:DanRollins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 60

Expert Comment

by:Kevin Cross
Dan,

Great article.  Found it very useful.

Thanks for the contribution.

Regards,

Kevin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month