Microsoft Script Debugger
The following are the main features of the Microsoft Script Debugger:
Dynamic view of HTML structure
Code is color coded
Ability to set breakpoints
Can step over, through, and out of each line of code
Immediate expression window
The MSSD is easy to use, but there are a few things to know that will have you debugging your code quicker in MSSD.
Starting the Debugger
The only way to start MSSD is to first open Internet Explorer and load the desired HTML source file. Then you can activate MSSD by choosing View, Source.
If MSSD isn't installed, viewing the source will open the source file in the Notepad editor.
To start the debugging process, choose Edit, Break at Next Statement from Internet Explorer, or choose Debug, Break at Next Statement from MSSD, and execute the script. This starts the debugger and stops it at the first statement in the current script.
The Break at Next Command
The Break at Next Statement command (which appears on the Script Debugger option of the View menu of Internet Explorer and the Debug menu of MSSD) is similar to a step command, in that the debugger executes the next statement in the script and then breaks, except that you can also use it when you are not currently running the script.
An expression can be evaluated with the aid of MSSD's immediate window and the following two methods:
Debug.write(string)This method writes a specified string, which is often the value of a variable, to the immediate window with no intervening spaces or characters between each string.
Debug.writeln([string])This method is identical to the preceding method, except that a newline character is inserted after each string. Also, the string argument is optional. If it's omitted, only a newline character is written to the immediate window.
A Final Word on the Microsoft Script Debugger
However, MSSD has some limitations in that you need to switch frequently between Internet Explorer and MSSD to conduct debugging, and that you can't print source code. If you want more functionality than what MSSD offers, you might want to consider Microsoft's Visual InterDev 6.0. This product offers all the script-debugging features listed here, plus a full-blown Web developing environment.
At the least, MSSD is certainly a good tool to have in your arsenal, and it's a great value to boot because it's free. However, it can't take the place of writing solid code and systematically testing your code.
The alert() Method
Listing 3.9 Problem Code
Figure 3.2 There is a problem associated with setting the color of the car.
In Listing 3.10, alert() methods are used to debug the code. An alert() method is placed in each of the variable-setting functions to determine if each function is being executed. More alert() methods are placed before and after the if statement to show how the conditional evaluates.
Listing 3.10 Debugging Using the alert() Method
When Listing 3.10 is executed, the first alert box displayed shows that both variables are undefined before the execution of the if statement. The next alert box shows that the setType() function was executed. The final alert box shows the vehicle type set to "car", but the color is still undefined after the if statement, as you can see in Figure 3.3. What happened to the setColor() function? The alert() method in the setColor() function was never executed, which lets us know that the setColor() function was never called from within the if statement.
Figure 3.3 Debugging using the alert() method.
If the first argument in a logical OR operation evaluates to true, the second argument is never evaluated. Because the setType() function returned true, the setColor() function was never executed. The problem is easily corrected by simply changing the logical OR operator to a logical AND operator.
After your code checks out, don't forget to remove the alert() methods that you added for debugging or else your program will display these alerts to those who use your scripts.