Debugging JavaScripts
There's no getting around it: debugging is a tedious business, but a necessary one. With JavaScript, you face two distinct challenges:
- getting the script to work in any browser
- getting the script to work in all browsers that support JS (unless you specify which version of JavaScript you're writing and force browsers that don't support it to ignore it)
For the latter problem, which is not insignificant, there is no solution but to try your script on all possible versions of all browsers that support JavaScript, or at least all the ones you think your site visitors are likely to have. Before you think about this, though, you want to make sure you take care of the first task, which is to make sure the script runs properly in some browser. Here are a few tips to take some the pain out the process:
- do your testing on either IE 4.0 or Netscape 4.0 (they have better built-in debugging than earlier versions)
- trust the debugging messages, but remember that they may be pointing to a symptom of the problem rather than the main cause
- remember that IE generally is less strictly case-sensitive than Netscape; if your script runs in IE, but not Netscape, that's something to look for
- look for unclosed parentheses, braces, and quotation marks
- if you have quotation marks nested within other quotation marks (in nested strings), remember that you have to alternate between single and double quotation marks with each level of nesting
- if you're parsing strings derived from built-in object properties, the strings may need to be run through the unescape function to convert them to plain ASCII first
- make sure you haven't used an "=" instead of a "==" for a logical comparison
- for stubborn bugs, it may help to write every value to the document as your get or construct it, to make sure what you get is what you think you're getting (this is a good technique for tracking down problems that only appear in certain browsers also, as the cause is often that the browser is returning a value in a different form than you expected)
|