Microsoft® Script Debugger provides you with a comprehensive debugging environment for testing and correcting errors in your Web document scripts. You can use the debugger to test scripts written in Microsoft Visual Basic® Scripting Edition (VBScript) and Microsoft JScript, as well as programs written in Sun Microsystems® Java™ and run using the Microsoft Java Virtual Machine (VM). If you have installed an alternative scripting language, such as REXX or Perl, you can also debug scripts in that language.
You can use Microsoft Script Debugger to debug both client scripts and server scripts:
Microsoft Script Debugger works the way many debuggers do, by allowing you to:
When you work with Microsoft Script Debugger, you see the script you are debugging in a window. The following illustration shows a typical debugging session:
Note You cannot change text in the window - documents are read-only while you are debugging. However, you can save the document under a new name, then open the new document and modify it. For details, see Managing Documents.
To control program execution, you set breakpoints, which are places where the script should pause and call the debugger. When you set breakpoints, the line containing the breakpoint is highlighted in the editing window, as shown in the following illustration:
After setting breakpoints, you run the document containing the script. When execution reaches the first breakpoint, the script pauses and starts the Script Debugger. The current line is marked in the editing window, as shown in the following illustration:
After reaching a breakpoint, you can execute, or step into, individual lines in your script. As you step through lines, the script executes and you can see the effect of each line.
If you reach a point in your script that calls another procedure (a function, subroutine, or the script associated with an object or applet) you enter (step into) the procedure or run (step over) it and stop at the next line. At any point, you can jump to the end (step out) of the current procedure and carry on with the rest of the script.
While debugging, you can view and change the values of variables or properties. Because you are making changes directly to a running script, you can affect the way the script executes by changing values in it. After changing values, you can continue with the script and see the effect of your change.
To view and change values, you use the Command window. You can evaluate any expression in the window and can enter script commands and see their effect. You enter commands in the language of the script currently executing. The following illustration shows how you can get the value of a variable using VBScript:
And the following shows the same, but in JScript:
The Call Stack window allows you to trace the procedures and functions that are currently executing. While debugging you can view the flow of control from procedure to procedure. You can also select a procedure in the call stack and jump directly to it, and then continue running the script. The following illustration shows a typical call stack:
See Also
For general information about debugging, see the following topics:
For specific information about using the Script Debugger, see the following topics.
For details about the Script Debugger toolbar, menus, and windows, see the following topics:
Errors in scripts can take three forms: syntax errors, runtime errors, and logic errors.
Syntax errors occur if you mistype a keyword, forget to close a multiline command (such as DO ... LOOP), or introduce a similar syntax error. If your script includes a syntax error, the script will not execute, and an error message is generated.
Note In some programming environments, syntax errors are referred to as pre-processor, compilation, or compile-time errors.
The result of a syntax error depends on what type of debugging you are doing:
Tip The text of the error message might not always reflect the exact error. For tips about finding errors, see Debugging Tips and Tricks.
If you write programs in other environments, you will notice that syntax errors are handled differently when you are writing HTML scripts. In Visual Basic, for example, syntax errors are caught by the interpreter as soon as you leave the line in which the error appears. However, when you write scripts for HTML documents, the code is not interpreted until the code is executed - namely, when the document is requested by a browser.
A runtime error occurs when a command attempts to perform an action that is invalid. For example, a runtime error occurs if you try to perform a calculation using a variable that has not been initialized.
The conditions that result in runtime errors depends on the language you are scripting with. A condition that might cause a runtime error in VBScript might not cause an error in JScript. For example, attempting to divide by zero results in a runtime error in VBScript, but not in JScript.
If there is a runtime error in a server script, and debugging is enabled for that application, the server starts the debugger, which displays an error message:
If a server script contains a runtime error and debugging is not enabled for that application, the error message text will appear in the client browser:
If a runtime error occurs in a client script, you might see an error such as the following when the document is loaded into Microsoft Internet Explorer:
The result of a runtime error is similar to that of a syntax error: if you are debugging on the server, an error message appears in the script debugger on the server. Otherwise, error text appears in the client browser.
Depending on the language you are using, you might be able to include statements in your scripts to trap runtime errors and run your own error procedures. For example, in VBScript, you can use the ON ERROR statement to establish error trapping. For more details, see the documentation for your scripting language.
You commonly use the Script Debugger to help you track down logic errors. A logic error occurs when a script executes without errors, but the results are not what you intended. For example, a script might prompt the user for a password. If the script is supposed to prevent the user from proceeding without a valid password, but it doesn't, you have a logic error in the script. In the case, you might use the Script Debugger to run the document, and then step through each line to see why the script doesn't execute the logic the way you intended.
See Also
Understanding how scripts in ASP pages are processed and how errors are handled can help you debug server scripts successfully. To debug server scripts - scripts in ASP pages - you run Microsoft Script Debugger on Microsoft Internet Information Server (IIS), version 4.0 or higher, and attach it to a currently running document.
Note To debug client scripts, you must run Microsoft Script Debugger on the same computer that is running Microsoft Internet Explorer. For more details, see Client Script Debugging.
Script in an ASP page is not event-driven. Instead, when the page is requested, the server reads the page and processes all server script from top to bottom. Both syntax errors and runtime errors are caught immediately.
A special case for server scripts is the Global.asa file. The Application_OnStart and Session_OnStart procedures in this file are executed only once for an application and for a session. Therefore, you must embed debugging statements in the file. For details, see Debugging the Global.asa File.
To use Microsoft Script Debugger, you must be sure that the debugger is enabled. For details about enabling and disabling the debugger for IIS, see "Debugging ASP Scripts" in the IIS documentation.
If the debugger is enabled:
Note If the debugger is enabled on the server, someone must attend the server to view and acknowledge error messages.
If the debugger is disabled:
See Also
Understanding how scripts in HTML documents are processed and how errors are handled can help you debug client scripts successfully. To debug scripts in HTML documents, you run Microsoft Script Debugger on your client computer and attach it to a document currently running in Microsoft Internet Explorer or Outlook.
Note To debug server scripts - scripts in ASP pages - you must run Microsoft Script Debugger on the same computer that is running Microsoft Internet Information Server (IIS). For details, see Server Script Debugging .
Client scripts (scripts in an HTML page) are processed by a browser such as Microsoft Internet Explorer. The browser calls the appropriate runtime module to process VBScript scripts, JScript scripts, or Java programs.
Client scripts are initially parsed when the HTML document is loaded into the browser. During this parsing phase, the browser reports any syntax errors that it finds.
After parsing a section of script, the browser executes it. Global or inline scripts, which are scripts that are not part of an event-handling subroutine or function, are executed immediately. Event-handling subroutines or functions, and procedures that are called by other procedures, are parsed immediately but are not executed until triggered by an event or called by another procedure.
If a runtime error occurs when a client script runs, an error message is displayed and script execution stops. The remainder of the HTML document can continue to run. If the script containing the error is called again, the error message is displayed again.
If an error occurs in a client script, Microsoft Internet Explorer displays an error message indicating the error and the line on which it was found, as in the following illustration:
If you choose to view the source, Internet Explorer starts the debugger and displays the script in a read-only window. You can navigate in the document, set breakpoints, and run the document again. However, you cannot directly edit the source code in the debugger. If you have access to the original document file (the .htm file), you can make changes to that file, and then reload it in the browser.
Note If you are debugging a client script generated by an ASP page, the line numbers reported in error messages refer to lines in the HTML document currently displayed in the browser. These usually do not correspond to line numbers in the original ASP page, because server script does not appear in the HTML output of an ASP page.
See Also
The following tips can help you find errors in your scripts more easily while you are using Microsoft Script Debugger.
The following table lists common error messages that can occur while you are creating scripts, and possible explanations.
Common Wrrors While Running Scripts
Error |
Possible explanation |
|
|
|
|
|
|
|
|
|
|
The Script Debugger can help you quickly find and fix errors in your scripts. Even so, it is still easier to create scripts if you prevent bugs in the first place. Below you can find information about:
Studies show that developers tend to make the same types of mistakes, no matter what language they are using or what program they are writing. The following table lists common problems that lead to bugs and suggestions for avoiding them.
Common Sources of Programming Errors
Problem |
Suggestion for avoiding |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following tips might help you prevent problems while scripting.
Do While Not RecordSet.EOF ' Process here RecordSet.MoveNext Loop
You can make Microsoft Script Debugger available in different ways, depending on how it will be used.
If you are debugging scripts on the client side, make sure that you have Internet Explorer and the Script Debugger installed properly. If you are debugging scripts on the server side, you must explicitly enable debugging mode for each ASP application you want to use the debugger with, or you cannot invoke the Script Debugger. For information about client-side and server-side scripts, see Debugging Scripts. For details about enabling and disabling the debugger for IIS, see "Debugging ASP Scripts" in the IIS documentation.
The Script Debugger can be started in these ways:
If the error is in a server script and debugging is enabled for the ASP-based application, the server opens the Script Debugger automatically and displays an error message, as shown in the following illustration:
Note Debugging scripts in the Global.asa file works slightly differently. For details, see Debugging the Global.asa File.
To start the script debugger manually
To start the script debugger by viewing source
After you have started the Script Debugger, you set one or more breakpoints, return to the document in Internet Explorer, and perform an action. For more information, see Setting Breakpoints.
See Also
Choosing a Document to Work With
You can debug scripts in any document currently executing in Internet Explorer, Microsoft Outlook™, or another ActiveX debugging host. If you are debugging a Java program, you can choose the name of the Java function that you want to debug.
To choose a document to work with
- or -
Click on the Debug
toolbar.
A list of open documents for that application appears in the text area below the drop-down list.
Note The Global.asa file is not included in the list of running documents. For information about how to debug scripts in the Global.asa file, see Debugging the Global.asa File.
See Also
Setting a breakpoint allows you to specify where in a script you want to stop execution and start the debugger. For example, you might want to start the debugger at the beginning of a procedure or before a line that you suspect might cause an error.
You can set breakpoints on specific lines, which helps you to pinpoint problems at particular points in your scripts. You can also set a breakpoint on the first executable line in an HTML document or ASP page, which enables you to start debugging as soon the first line of script is executed. Setting a breakpoint for the first script statement in a document is particularly useful for debugging scripts that are executed as part of a document's OnLoad event and for debugging ASP pages.
To set a breakpoint
- or -
For server scripts, start the debugger as a standalone application, and then choose the document to work with. For details, see Starting the Script Debugger and Choosing a Document to Work With.
Note If you are setting a breakpoint in a Java program, select the entire statement.
- or -
Click on the Debug
toolbar.
The line where you set the breakpoint is displayed in red to indicate that it is a breakpoint.
- or -
For server scripts, refresh the document in the browser.
The debugger stops at the first breakpoint it encounters. You can now execute (step through) lines one by one. See Stepping Through Scripts.
Setting a breakpoint for the first script statement in a document is particularly useful for debugging scripts that are executed as part of a document's OnLoad event. The script in the handler for this event has already been executed by the time the HTML document has completed loading, and any breakpoints set after the HTML document has completed loading are lost when the document is refreshed. Therefore, use the following procedure to debug script statements in an OnLoad handler.
To set a breakpoint for the first script statement in a document
The Microsoft Script Debugger opens with the current document visible in Source view.
- or -
Click on the Debug
toolbar.
The debugger stops at the first script line that is executed.
Note You cannot use this method to set breakpoints in the Global.asa file. For information about how to debug scripts in the Global.asa file, see Debugging the Global.asa File.
See Also
If you no longer want to start the debugger at a breakpoint in your script, you can clear the breakpoint.
Note Breakpoints are cleared automatically if you reload a document in Internet Explorer.
To clear a single breakpoint
- or -
Click on the Debug
toolbar.
The line is redisplayed in normal text.
If you are finished debugging a script, you can clear all its breakpoints.
To clear all breakpoints
- or -
Click on the Debug
toolbar.
Lines that were marked in red as breakpoints are displayed in normal text.
See Also
You can use Microsoft Script Debugger to execute (step through) individual lines in a script. You can then view and check the results of each script line.
If the script calls another procedure or function, you can step into the procedure. Alternatively, you can step over the procedure. In that case, the procedure is executed, and when it finishes, you are left on the line immediately following the call to that procedure.
To step through scripts
- or -
Switch to Internet Explorer.
Server scripts: Refresh the document.
The Script Debugger stops at the first breakpoint, displaying it in the editing window:
Command |
Button |
Action |
Step Into |
|
Moves to the next script line. If the script calls another procedure, Step Into proceeds through the lines of that procedure. |
Step Over |
|
Skips called procedures. The procedure is executed, but the debugger does not step through its individual lines. Instead, the debugger moves to the next line in the current procedure after executing the called procedure. |
The debugger moves to the next statement following the procedure.
See Also
While debugging scripts, you can see a list of procedures (subroutines, functions, event handlers, and other scripts) that are currently loaded. Each time a procedure is called, the name of the called procedure is added to the top of the call stack. When the procedure ends, its name is removed from the call stack.
Viewing the call stack can help you trace the course of execution through a series of nested procedures. You can also use the call stack to navigate within the document to a specific procedure. When you navigate to another procedure, you can set breakpoints there, change variable values, and so on.
However, using the call stack to navigate to another procedure does not change the currently executing line. Even if you set a breakpoint or variable value in another procedure, execution resumes from the line where it was halted. If you execute a command in the Command window after navigating to another procedure in the call stack, the context of the command is still the procedure where you are halted.
When you are debugging Java programs, the call stack includes currently loaded Java methods. You will therefore see a mixture of procedure names from VBScript or JScript and the names of Java methods.
To view the call stack
- or -
Click on the Debug
toolbar.
To jump to a specific procedure
See Also
While a script is running, you can use the Command window to view and change the values of variables and properties in the script. The Command window is live in the context of the current script, so that changes you make there can directly affect the currently executing script.
The commands, values, and properties that you can use in the Command window depend on the environment in which the debugger is running:
You can execute script commands any time that you are at a debugger breakpoint or have stepped from a breakpoint to other statements.
To view and change variable values
For information about debugging the Global.asa, see Debugging the Global.asa File.
- or -
Click on the Debug
toolbar.
The value of the variable is displayed in the window. The following illustration shows an example in JScript:
Document.Title = Document.Title & " (Debugging)" txtLoginName = "Admin"
See Also
You can use the Command window to execute script commands directly - that is, without having to place them in a script and then run the script's document. By running script commands in the Command window, you can:
When you use the Command window to execute script commands, you are working in the same environment as the current scripts, and can therefore affect the way the script runs. For example, you can use commands in the Command window to change the values of variables or properties and therefore change the way a script executes. You can also create new objects and properties in the Command window exactly the way you would in a script.
You can execute script commands any time that you are at a debugger breakpoint or have stepped from a breakpoint to other statements.
When using the Command window, use the language of the currently executing procedure. For details about script commands, refer to the documentation for your scripting language.
To execute script commands immediately
- or -
Click on the Debug
toolbar.
When you enter commands into the Command window, error messages are not sent to the browser. Instead, the Command window handles the error itself by displaying a message, as shown in the following illustration:
See Also
Choosing a Document to Work With
You can manage HTML documents or ASP pages using the HTML source editor in the Microsoft Script Debugger. The editor allows you to:
When you are debugging a document, the document is displayed in the Script Debugger window as a read-only document. You therefore cannot directly change the document's source code. However, you can save the document under a new name, and then open the new document, make changes, save it again, and then reload it in the browser.
You manage files using commands on the File menu. For details, see File menu.
Debugging the Global.asa differs from debugging ASP files.
If there is an error in the Global.asa file (either a syntax error or runtime error), the server stops the procedure containing the error. If the Script Debugger is enabled for that ASP-based application, the server starts the debugger and displays an error message. If the Script Debugger is not enabled, an error message is sent to the client browser. In either case, the procedure containing the error stops. If you fix the error in the Global.asa file and request a page from the application again, the procedures in the Global.asa are executed again when the ASP-based application is restarted.
To control when the Script Debugger is called from the Global.asa file, include a statement that starts the debugger explicitly, such as Stop in VBScript or Debugger in JScript. Place the statement at the beginning of the procedure, before any statements that you will want to step through.
When the application starts or when a user starts a new session, the corresponding procedure in the Global.asa file will run. The Script Debugger will start when the Stop or Debugger is executed. You can then step through the procedure normally, use the Command window to evaluate expressions and set values, and so on.
However, you cannot stop and restart the script by refreshing the Global.asa file. If you want to rerun the Application_OnStart script, you must stop the application and then restart it. If you want to rerun the Session_OnStart script, you can restart your browser, which starts a new session and reruns the Session_OnStart procedure. Alternatively, you can request a page that includes a script to call the Session object's Abandon method, and then start a new session.
You can use the Microsoft Script Debugger to debug your Java programs (applets and components). For the most part, debugging Java is identical to debugging VBScript or JScript scripts. In a few instances, however, you must follow special procedures.
This topic provides the following information about debugging Java programs:
To use the Microsoft Script Debugger to debug Java, you must be using Microsoft SDK for Java 2.0. This version of Java is enabled for ActiveX debugging. You can download and install a preview release of the SDK from the Microsoft Web site at this address:
http://www.microsoft.com/java/
You must explicitly enable Java debugging in Microsoft Script Debugger.
To enable Java debugging
To be able to debug Java code, you must compile it with a debug option. To enable this option:
\BIN\JVC.EXE
To debug Java programs, the Java applets or components must be available to the debugger.
To start the Script Debugger for Java programs
Microsoft Script Debugger opens with the HTML document containing your applet loaded.
Tip If you double-click a class name in the Running Documents window and see no result, check that the path to the source code is correct.
For more information about starting the debugger, see Starting the Script Debugger and Choosing a Document to Work With.
Depending on the compiler you used, when you are setting breakpoints in a Java program, you might need to select the entire Java code statement. For more information, see Setting Breakpoints.
When you are debugging Java programs, the Call Stack window displays the names of procedures (if any) in the current scripting language as well as the names of any Java methods currently active. For more information, see Viewing the Call Stack.
Using the Command window in Microsoft Script Debugger, you can evaluate Java expressions the same way that you can when debugging with VBScript and JScript scripts.
For example, to examine the contents of a variable called nLimit, type nLimit into the command window. The value of the variable is immediately displayed on the next line.
For more information, see Executing Script Commands and Viewing and Changing Values.
You can use the following buttons on the Debug toolbar as shortcuts for common debugging tasks.
Button |
Name |
|
Start or continue execution of the current document. |
|
Stop debugging. |
|
Break into the debugger at the next (or first) statement in a document. For more information, see Setting Breakpoints. |
|
Step into (execute) the next statement. For more information, see Stepping Through Scripts. |
|
Step over (skip) the next statement. For more information, see Stepping Through Scripts. |
|
Step out of (quit) the current function. For more information, see Stepping Through Scripts. |
|
Insert or remove a breakpoint. For more information, see Setting Breakpoints. |
|
Remove all breakpoints. For more information, see Clearing Breakpoints. |
|
Display Running Documents window and select a document to debug. For more information, see Choosing a Document to Work With. |
|
Display the call stack to view and navigate to nested procedures. For more information, see Viewing the Call Stack. |
|
Open Command window to type in commands or expressions, or to view and change variable values. For more information, see Executing Script Commands and Viewing and Changing Values. |
The File menu contains commands that enable you to manage files while in the debugger.
New Creates a new, blank document that you can edit using the Script Debugger editor.
Open Opens a document so you can edit it.
Close Closes the current document. If you have not saved the document since making a change, the Script Debugger prompts you to save it before closing it.
Save Saves the current document under its original name. If you are debugging a document, that document is read-only, and you cannot save it under its original name.
Save As Saves the current document under a new name. If you are debugging a document and want to save it, you must choose this command.
Save All Saves all currently open documents that are not read-only.
Exit Closes the Script Debugger.
See Also
The Debug menu contains commands that enable you to manage how scripts are executed while you are debugging.
Run Causes execution to resume from the current statement.
Stop Debugging Runs the current script to the end without stopping at any breakpoints.
Break at Next Statement Causes execution to stop at the next scripting statement. You typically choose this command, and then refresh the document in the browser.
Step Into Executes one statement at a time from the current execution point. If the statement is a call to a procedure, the next statement displayed is the first statement in the procedure. If there is no current execution point, the Step Into command might appear to do nothing until you perform an action that triggers a script, such as clicking a button.
Step Over Executes one statement from the current execution point. However, if the current statement contains a call to a procedure, the Step Over command executes the procedure as a unit, and then steps to the next statement in the current procedure. Therefore, the next statement displayed is the next statement in the current procedure even if the current statement is a call to another procedure.
Step Out Executes the remaining lines of the current procedure. The next statement displayed is the statement following the procedure call. All of the script is executed between the current and the final execution points.
Toggle Breakpoint Sets or removes a breakpoint at the current line. You cannot set a breakpoint on lines containing nonexecutable code such as comments, declaration statements, or blank lines.
Clear All Breakpoints Removes all breakpoints in your script. Your script can still interrupt execution, however, if you have embedded a Stop (VBScript) or Debugger (JScript or Java) statements, or if a runtime error occurs in your code.
See Also
The Running Documents window displays a list of documents that can currently be debugged.
To display this window, choose Running Documents from the View menu.
Running Documents window
The combo box at the top contains a list of applications that are currently running documents that you can debug.
In the tree view below the combo box, each node represents an application. Click the plus sign (+) next to the node to display a list of documents to debug, and then select the document you want to work with.
See Also
Choosing a Document to Work With
The Call Stack window displays a list of active procedure calls.
To display this window, choose Call Stack from the View menu.
Call Stack window
When a procedure is called, the debugger adds its name to the list of in the Call Stack window. When a procedure finishes and returns control to the calling procedure, the debugger removes its name from the list in the Call Stack window.
Double-click a procedure in the list to see the place in code where it was called from.
The Call Stack window is a floating window that isn't affected when other windows are repositioned using the Cascade, Tile Horizontally, or Tile Vertically command on the Window menu.
See Also
The Command Window displays information resulting from debugging statements in your scripts or from commands typed directly into the window.
To display this window, choose Command Window from the View menu.
Viewing variable value in Command window (JScript)
Use the Command window to:
When entering commands in the Command window, use the syntax for the current scripting language.
See Also
Contains options for debugging Java programs.
To display this window, choose Options from the View menu.
Enable Java Debugging Choose this option to enable the debugger to step through Java code. The Java source files must be available. For details, see Debugging Java Programs.
Source Path Specify the root path on the server for your Java source files. The debugger will search this path for source code.