HTML and CSS Reference
In-Depth Information
Understanding JavaScript Syntax
Besides always including semicolons at the end of each statement, there are some other
syntax rules you should keep in mind when writing JavaScript statements. JavaScript is
case sensitive, so you must pay attention to whether or not letters are capitalized. For
example, the following statements are not equivalent as far as JavaScript is concerned:
document.write(“</a>”);
Document.write(“</a>”);
The first command writes the HTML tag </a> to a Web page document. The second
command is not recognized by browsers as a legitimate command and results in an
error message. Figure 10-12 shows the error message generated by the Internet Explorer
browser. The browser does not recognize the word Document (as opposed to document )
and so cannot process the command. You'll examine how to handle this type of error
later in this tutorial.
Figure 10-12
error message resulting from improper case
there is no Document
object in JavaScript
error message
Like HTML, JavaScript ignores most occurrences of extra white space, so you can
indent your code to make it easier to read. You can see examples of this in Figure 10-10,
where the newly entered statements are indented several spaces to make the commands
stand out from the opening and closing <script> tags.
However, unlike HTML, you must be careful about line breaks occurring within a
statement. A line break cannot be placed within the name of a JavaScript command or
within a quoted text string without causing an error in the code. For example, the follow-
ing line break is not allowed:
document.write(“<aƒhref=' mailto:cadler@mpl.gov'>
cadler@mpl.gov
</a>”);
It is good practice not to break a statement into several lines if you can avoid it. If you
must break a long statement into several lines, you can indicate that the statement con-
tinues on the next line using a backslash, as follows:
document.write(“<aƒhref=' mailto:cadler@mpl.gov'>ƒ\
cadler@mpl.govƒ\
</a>”);
Search WWH ::




Custom Search