Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Debugging JavaScript programs

A quick reference guide to finding errors without the aid of a debugging utility

  • Print
  • Feedback

Page 5 of 7

<SCRIPT>
document.write ("Hello<P>");
document.test.box.value = "Hello again";
</SCRIPT>
<FORM NAME="test">
<INPUT TYPE="text" NAME="box">
</FORM>

5. Using the wrong object hierarchy with forms

A common problem is referring to a form as a property of a window. This results in a confusing error -- "formname has no properties" (formname is the name of the form). It's confusing because the form is obviously an object with properties. For example, the following JavaScript says "test has no properties," yet it's obvious the test form is a valid object with properties:

<FORM NAME="test">
<INPUT TYPE="text" NAME="box">
</FORM>
<SCRIPT>
window.test.box.value = "Hello";
</SCRIPT>

The fix is to reference the form as a document property, as in:

document.test.box.value = "Hello";

6. Using string methods with objects that don't support those methods

Some of JavaScript's objects return values that look like strings, but they are not true strings. For instance, the following displays the name of the current document URL. Although the return value looks like a string, it is not, and therefore doesn't support any string object methods or properties, such as indexOf or toUpperCase.

var ret = location;
alert (ret.toUpperCase());  // results in an error

You need to convert the return value to a true string, then use the string methods/properties with the converted value. The toString method offers one general approach you can often use:

var ret = location.toString();
alert (ret.toUpperCase());  // no error this time

JavaScript error messages

Here is a list of the more common error messages you are likely to encounter when working with JavaScript. The messages are listed in alphabetical order in two sections: load-time errors (errors that occur when a script is loaded into the browser) and runtime errors (errors that occur when you play a script).

Load-time error messages

These error messages appear when an HTML document containing JavaScript is first loaded.

function does not always return a value

JavaScript found one or more return statements in a function that returned a value, and at least one return statement that didn't return anything. For example, the following script will result in an error in JavaScript:

function myBadFunction (val)  {
    if (val==0)
    return;
else
    return ("some text")
}

To fix this error, make sure all of the return statements are the same. All should return either something or nothing.

identifier is a reserved word

You tried to name a variable or object with a word that is reserved by JavaScript. Some words are reserved because they are used by JavaScript as names for statements, functions, methods, or objects. Other words are reserved for future use. These words are not currently used by JavaScript, but may be used some day. For example, the word int is reserved for future use. Using int as the name of a variable results in the "identifier is a reserved word" error:

int = "this is a test";

missing ( ... missing ) ...

These errors (the ... denotes additional error message text) occur when you've forgotten to add the appropriate opening or closing parenthesis in your JavaScript code. For example, the error "missing ) after for-loop control" occurs with the following:

  • Print
  • Feedback