Java Reference
In-Depth Information
This solution is based on similar questions in previous chapters, such as Chapter 10, which displayed a
randomly selected image. In this case you display a different image in the page each time the user visits
it, as far as our selection of images allows.
You've seen the cookie setting and reading functions before in the chapter, so let's look at the new code.
Store the number of the previously displayed image in a cookie named displayedImages. The next
image you display is that image number + 1. Once all of the images have been displayed, you start
again at 1. If the user has never been to the web site, no cookie will exist so null will be returned from
getCookieValue(), in which case you set imageNumber to 1.
Most of the code is fairly self-explanatory, except perhaps this line:
if (totalImages == imageNumber)
{
setCookie(“displayedImages”,”“,”“,”Mon, 1 Jan 1970 00:00:00”)
}
What this bit of code does is delete the cookie by setting the cookie's expiration date to a date that has
already passed.
Chapter 12
Exercise 1 Question
Here's some HTML code that creates a table. Re-create this table using only JavaScript and the core DOM
objects to generate the HTML. Test your code in all browsers available to you to make sure it works in
them. Hint: Comment each line as you write it to keep track of where you are in the tree structure, and
create a new variable for every element on the page (for example, not just one for each of the TD cells
but nine variables).
<table>
<thead>
<tr>
<td>Car</td>
<td>Top Speed</td>
<td>Price</td>
</tr>
</thead>
<tbody>
<tr>
<td>Chevrolet</td>
<td>120mph</td>
<td>$10,000</td>
</tr>
<tr>
<td>Pontiac</td>
Search WWH ::




Custom Search