HTML and CSS Reference
In-Depth Information
Table 9-5 shows the naming conventions for a function name with valid and invalid
examples. A function name must begin with a letter or an underscore; it may contain
numerals, but may not contain any spaces, punctuation (such as periods or commas), or
reserved words. Data, as variables, are passed to a function in order for the function to
process data and return a result.
Table 9-5 Valid Function Names
Rule
Valid Function Names
Invalid Function Names
Must start with a letter or
underscore
verifyForm()
_3Ddisplay()
3Ddisplay()
No periods allowed or other
punctuation allowed
get_Cookie()
make.cookie()
valid,form()
No spaces allowed
calcPayment()
calc payment()
No hyphens allowed
popWind()
pop_Wind()
pop-upWindow()
Plan
Ahead
To display a message with the current date and the number of days until a specific future
date, you must:
Create the <script> section in the <head> section
•
. In this project, the <script> sections will
be placed in the <head> section.
Define variables
•
. Using variables allows you to work with the system date, which
changes daily, to calculate the number of days from today to the Midwest Bridal Expo.
Calculate the number of days until the Midwest Bridal Expo
•
. To calculate the number
of days to the Wedding Expo, you write code to subtract the current date from the
future date.
Display a message string
•
. Use the innerHTML property to place the message on the
Web page. The innerHTML property allows you to change the contents of the text that
appears between an opening and closing of an HTML tag.
Close the <script> section
•
. All HTML tags must have a closing tag. If you fail to close
the <script> section, you will have undesired results.
Add the event handlers
•
. The event handlers call the function to display a dynamic
message when the Web page loads.
Inserting <script> Tags in the <head> Section
Although JavaScript code can be placed anywhere in the HTML code in a <script>
section, developers follow the coding practice and place user-defined functions and vari-
ables in the <head> section. Placing the JavaScript code in the <head> section ensures that
all the JavaScript is loaded and interpreted before the user can begin using the Web page.
In this chapter, you will only use JavaScript features that work in the latest version
of Microsoft Internet Explorer. JavaScript sections always start with a <script> tag, which
indicates the language being used. Similar to other HTML tags, the JavaScript <script>
tag has a start <script> tag and an end </script> tag.
The general form of the script tag is shown in Table 9-6 on the next page. The
<script> tag supports several attributes, including src, type, and defer. In the past, the start
<script> tag was written as <script language="JavaScript">, but, as noted in the Table 9-6
example, the preferred style is to use the type attribute. If the type or language attribute is
omitted, most browsers default to JavaScript.
The <!-- HTML comment line after the <script> tag is used to hide the JavaScript
code in the event a browser does not recognize the JavaScript code. Like all other HTML
tags, it must be closed (using the format -->).
Script Sections
Many Web developers
recommend inserting
the start <script> tag
and the end </script>
tag immediately as the
Web page is modified
or constructed. The
important rule to
remember is that a
complete set of <script>
tags must be included,
both <script> and
</script>.