HTML and CSS Reference
In-Depth Information
1
line 19.
If necessary, click
code shown in
Table 9-17 on the
previous
page. Enter
the current
year's
Midwest Bridal
Expo date in line 19,
if necessary. Press
e n t e r at the end of
each complete line
of code.
Enter the JavaScript
future date object
from line 19
code to assign future date
to bridalExpo variable
insert current year
code to calculate number of
milliseconds between current
date and future date
JavaScript code
to convert
milliseconds
into days
insertion point after
pressing ENTER key twice
1000 milliseconds in a second
60 seconds in a minute
60 minutes in an hour
24 hours in a day
current date object
from line 9
Figure 9-6
After typing the last line in Table 9-17, press the
e n t e r key twice (Figure 9-6).
Can a date only be entered as “February 12, 2014” in the Date() object?
The date can also be entered as 2014, 1, 12 in the Date() object. Because the Date() object starts numbering the months
with 0, that means February is 1.
Compare what you typed to Figure 9-6. Correct any errors before proceeding.
What happens if I enter a date for the Midwest Bridal Expo that is in the past?
Entering a date that is in the past, prior to today's date, will result in a negative number in the countdown display.
Plan
Ahead
Using JavaScript to write dynamic text to a Web page.
Before writing dynamic text to a Web page, you need to determine what your message
will say and how you will format it for display. Most messages will be a combination of text
strings and variables. Text can be formatted using standard HTML tags or using inline styles.
Inline styles can be placed in <p> or <span> tags to format text.
Displaying Text and Variable Values
to a Web Page with innerHTML
After the number of days until the Midwest Bridal Expo has been calculated, this
data can be written on the Web page using JavaScript. To write data directly to the Web
page, JavaScript can use the write() or writeln() methods of the document object. These
methods, however, are not useful in user-defined functions located in the <head> section.
For this reason, most developers do not use the write() or writeln() methods and they
should be avoided if possible. Instead, to display text at a specific location, developers
use the innerHTML property in association with the id value of a <div> tag container.
The innerHTML property allows dynamic content to be placed in the container asso-
ciated with the unique tag id. (Note that the Firefox browser does not recognize the
innerHTML property.) Table 9-18 shows the general form of the innerHTML property.
JavaScript Strings
The recommended
maximum length of a
string is 256 characters
or bytes. This limit is
based on a limit of older
browsers.
JavaScript writeln()
Method
The writeln() method
works only in HTML tags
that either are sensitive
to the new line character
(“/n”) or the carriage
return, line feed, such as
the <textarea> tag.
Table 9-18 innerHTML Property
General form:
tag_id.innerHTML=“text string”
Comment:
where tag_id is the id attribute assigned to the designated container tag, and text string is any
combination of HTML tags, text, and variables
Examples:
displayCountDown.innerHTML = “Today is “+weekDay+” “+monthDate+” “+year+”. We have
“+daysToBridalExpo+” days until the Midwest Bridal Expo.“
 
Search WWH ::




Custom Search