|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Basic Debugging ToolsIf checking your script for common mistakes and obvious problems doesn't fix things, it's time to start debugging. This is the process of finding errors in a program and eliminating them. Some basic tools for debugging scripts are described in the following sections. Firefox's JavaScript ConsoleThe first thing you should do if your script doesn't work is check for error messages. In Firefox and other Mozilla-based browsers, the messages are not displayed by default, but are logged to the JavaScript console. To access the console, type javascript: in the browser's Location field or select Tools, JavaScript Console from the menu. The console displays the last few error messages that have occurred, as shown in Figure 16.1. Figure 16.1. The JavaScript console displays recent error messages.
Along with reading the error messages, you can use the console to type a JavaScript command or expression and see its results. This is useful if you need to make sure a line of your script uses the correct syntax. Displaying Error Messages in Internet ExplorerMicrosoft Internet Explorer 4.0 and later do not display JavaScript error messages by default. This can make browsing poorly written pages a more pleasant experience, but it can be frustrating to JavaScript programmers. To enable the display of error messages in Internet Explorer, select Internet Options from the Tools menu. Select the Advanced tab. In the list under Browsing, deselect the Disable Script Debugging option and enable the Display a Notification About Every Script Error option. If you haven't enabled the display of error messages, Internet Explorer still displays an error icon on the status line when an error occurs. You can double-click this icon to display the error message. Alert MessagesIf you're lucky, the error messages in the console will tell you how to fix your script. However, your script might not generate any error messages at allbut still fail to work correctly. In this case, the real debugging process begins. One useful debugging technique is to add temporary statements to your script to let you know what's going on. For example, you can use an alert statement to display the value of a variable. After you understand what's happening to the variable, you can figure out what's wrong with the script. By the Way You can also display debugging information in a separate browser window or frame. You can use document.write in some cases, but this only works when the document hasn't finished loading yet and thus isn't a reliable debugging tool. Using CommentsWhen all else fails, you can use JavaScript comments to eliminate portions of your script until the error goes away. If you do this carefully, you can pinpoint the place where the error occurred. You can use // to begin a single-line comment, or /* and */ around a section of any length. Using comments to temporarily turn off statements in a program or a script is called commenting out and is a common technique among programmers. By the Way JavaScript comments were introduced and described in more detail in Hour 3, "Getting Started with JavaScript Programming." Other Debugging ToolsAlthough you can use alert messages and a little common sense to quickly find a bug in a simple script, larger scripts can be difficult to debug. Here are a few tools you might find useful as you develop and debug larger JavaScript applications:
Did you Know? Appendix B, "Tools for JavaScript Developers," includes links to HTML validators, editors, and other debugging tools. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |