★ wanayoo — archive 1999 http://webdeveloper.com/javascript/js_heidi_debug_2.htmlNouvelle recherche | Portail wanayoo
internet.com
Library
Magazines

webreference.com

Java Boutique

Search Engine Watch

PC Webopedia

The Web Developer's Virtual Library

Free Web Developer Info! Library

JavaScript Programming: Debugging Part II

by Heidi Brumbaugh

A syntax error is asking for a "scwerdriver".

A logic error is asking for a flathead screwdriver, then trying to use it on a Philips screw.

Most other programming languages have a debugging environment that helps you zero in on errors. If you're programming JavaScript from a text editor and checking the results in a browser, however, debugging is difficult. The browsers are designed to be upwards compatible, so they may simply ignore instructions they don't understand. When they do report errors, the messages don't always tell you what you need to know to solve the problem.

You can follow along with the bugs.html file, which gives examples of many of the problems I talk about here.

When the browser gives you a syntax error, note the line number of the error. When you dig back into the script to see what's wrong, however, check for problems above and around that line for the problem as well. An error above the line, especially a missing ), >, or } could be causing the browser to misinterpret what happens next.

On a related note, be aware of "cascading" errors. This means that one error, such as an incorrectly defined function, can cause errors further down the line, for example, when you try to call the function. Correct the first error, then try to run the program again. Subsequent errors may be gone.

Here are some more tips on avoiding--and troubleshooting--programming errors.

  • Be careful about using reserved words--words that are part of JavaScript itself--as variable or function names.
  • Make sure your double and single quotation marks are matched correctly, especially in long expressions.
  • When you're writing code that generates HTML or JavaScript, it can be especially easy to make a mistake. Examine your output directly by running the code to create a new document, then clicking on View Page Source in the browser to see what actually got generated.

[Move on the Part III]


Web Developer® Site Feedback
Web Developer®
Copyright © 2000 internet.com Corporation. All rights reserved.

http://www.internet.com
Web Developer® Home Over a dozen topics in detail Live Chat Downloads Book and Product Reviews Threaded Discussions How-To/Articles/Links Developer Daily News Subscribe Search Corporate Information Advertise Events Publications internet.com Home