HTML and CSS Reference
In-Depth Information
Figure 10-27
Adding comments to the showeM() function
6. Close the mpl.htm file, saving your changes.
7. Reopen mpl.htm in your Web browser and verify that you have not introduced
any errors by adding comments to the showEM() function.
You show the commented version of the showEM() function to Kate. She agrees that it
will help her better remember the purpose of the function and how the function works.
Debugging Your JavaScript Programs
As you work with JavaScript, you will inevitably encounter scripts that fail to work
because of an error in the code. To fix a problem with a program, you need to debug
it. Debugging is the process of searching code to locate a source of trouble. To debug a
program, you must first determine the type of error present in your code.
There are three types of errors: load-time errors, run-time errors, and logical errors. A
load-time error occurs when a script is first loaded by a browser. As the page loads, the
browser reads through the code looking for mistakes in syntax. For example, suppose
you had neglected to include the closing parenthesis, as in the following command from
the showEM() function:
document.write(“</a>”;
In this case, you would be making a mistake in the syntax of the document.write()
method. When a load-time error is uncovered, the JavaScript interpreter halts loading
the script. Depending on the browser, an error message might also appear. Figure 10-28
shows the message generated by the above error in the Firefox Error Console. An error
message can include the line number and character number of the error. This does not
mean that the error occurred at this location in the document—the source of the trouble
could be much earlier in the script. The message simply indicates the location at which
the JavaScript interpreter was forced to cancel loading the script.
 
Search WWH ::




Custom Search