Online Documentation Server Web
Net technology
Web technology
Data bases
Other docs



. , - - , .

Previous Table of Contents Next

"string is not a number"

The erroneous line has an operator that requires a number, but a different type of variable has been found instead. You either have declared the variable (with var statement) but have not initialized it, or have not initialized it. It is always preferred to use parseInt() or parseFloat() to convert strings to numbers.

"string has no property named property"

JavaScript did not find the property provided for the object referenced on the specified line number. There may be few explanations for this bug. You are either trying to reference a property that does not exist for the relevant object, or failing to reference the right object. The latter often occurs when you forget to specify the index of an arrayed object. Look closely at the error message to see if it includes a reference to an entire array rather than just one of its elements. Common arrays are forms, links, and buttons.

"string has no property indexed by [i ]"

This error is the opposite of the previous one. Look at the last item and prove to yourself that it is not an element of an array. This is a very common mistake when references become very long and cumbersome, especially when creating radio buttons and select options. Just scan the reference elements one by one, and determine which are simple object names and which are arrays.

"string cannot be set by assignment"

You are either trying to assign a value to a read-only property, or assigning a value to an object reference which must be created via a constructor function, rather than by simple assignment.

"test for equality (==) mistyped as assignment (=)? Assuming equality test"

JavaScript is usually right here. You meant to use the equality comparison test (==) but had typed a single equal sign only.

"function does not always return a value"

JavaScript checks the organization of every function and verifies that a value is always returned. It is very common to focus on a single logic path while designing deeply nested if..else loops, and to overlook other cases in which the decision process falls through all the way to the bottom, without returning any value.

"access disallowed from scripts at Url_1 to documents at URL_2"

Cross-domain security, which is covered in Chapter 34, is being violated.

"Lengthy JavaScript still running. Continue?"

JavaScript provides a safeguard against the infamous bug of infinite loop. After processing a large number of cycles, JavaScript asks the user whether the script should continue. This safety net is for developers and users both. Developers use it for debugging infinite loops and freeing up the browser which would have been locked up forever otherwise. The safety net also protects users against JavaScripts harmful hackers.

"syntax error"

This is every compilers classic error message. The alert box provides you with the code extract and the pointer to the exact location of the error.

Manual Debugging Techniques

Sometimes, the error messages are of no help in giving directions towards finding the bug and you have to resort to manual and other techniques for debugging.

Match Those Tag Pairs

Before checking the code itself, go over the document carefully and check that all tags have matching pairs. Be sure to check that each tag is closed by a closing angle bracket.

View the Intermediate HTML Source

Just click in a frame to select it and choose Frame Source from the View menu. The displayed results include the HTML code that the script generates. Debugging often involves examining intermediate results. The HTML source code is an excellent means for verifying that the computer does what you intend it to. You can also print and save this JavaScript-written HTML.

Reopen the File

Sometimes reloading a URL does not free the browser memory from a bug you are trying to debug. Try reopening the file via the File menu. It may clear the browsers memory completely and reload the fixed version of your source file. If this does not help, try quitting the browser and restart it again. Rebooting the computer may also help if you are still frustrated with not being able to load your fixed source code.

Good Old Print Messages

Senior programmers may identify with this method, very popular in the days when we did not have debuggers. Just put alert dialog boxes in your script with a brief message that you will recognize (such as alert(Just before calling function xyz)). These dialog boxes will tell exactly which parts are working and which parts are disconnected and cannot be reached. You can either work your way from top to bottom or use the binary search mechanism. This method is very popular in classic searching and sorting algorithm. Insert an alert dialog box in the middle of the script. If the flow reaches this point of the script, then focus your effort on the second half, since the problem is somewhere there. If the flow does not reach the scripts midpoint, insert an alert box in the middle of the first half, and keep going until you quarantine the bug.

Comment Out Statements

Sometimes, the line number provided by the error message is not exactly the culprit one. To find out the exact one, start commenting the lines, one by one, starting from the given line number. Reload the source file after every additional commented line and observe when the error message goes away (and usually substituted with the next in order). At this stage, you know exactly which is the offending source line.

Watching and Tracing Variables

Watching and tracing variables, as well as single-stepping, are among the most powerful features of any programming environment. Most serious bugs occur when a variable holds an unexpected value, and the only way to find it is to single-step through the code and examine every variable upon its assignment. Unfortunately, JavaScript does not support these basic features, and you need to mimic them via alert boxes. Every time you change a variable by an assignment or invocation of string, math, or date method, insert an alert() method on the following line, and show the content of the variable in it. Repeat this sequence of edit, save, switch, and reload until you find a variable content which is not what you have expected.

Previous Table of Contents Next

With any suggestions or questions please feel free to contact us