home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Before JavaScript, a task such as this would have been handled at the server. Each hit would have involved having the server run some type of script or program to read the user’s cookies and generate his page on-the-fly. With JavaScript, all this processing takes place on the client’s browser. The server just downloads the static page—and it might not even do that because the page might come from the client’s local cache. When the page is loaded, all the links, selected or not, are sent. The client, with the help of cookies and JavaScript, decides which ones to show the user.

This program makes use of three cookies. The Favorites cookie contains a unique code for each favored link. The ViewAll cookie toggles between showing the user’s favorites and all possible links. The program may also display either of two pages: one for displaying the selected links, and the other for changing the configuration and options. When the ShowOptions cookie is set, the Options selection page is displayed. Otherwise, the regular page is shown.

When the screen shown in Figure 22.1 is displayed after the page is first loaded, the value of the document cookie is

Favorites=null; ViewAll=T

This indicates that no favorites have been selected, but that all the options should be displayed. If View Favorites is clicked at this point, then the Document cookie will be

Favorites=null

and the screen shown in Figure 22.2 will be displayed—empty, because no favorites have been selected yet. Clicking the Select Personal Favorites button gets the screen shown in Figure 22.3, where favorites can be selected from the list of choices. One such selection might result in the Favorites list shown in Figure 22.4, which has the document cookie value of

Favorites=null%3Ccdilb%3E%3Cmjod%3E%3Cmgunther%3E%3Csyah%3E%3Csav%3E


NOTE:  You may notice in Figures 22.2 and 22.4 that the current contents of the document cookie are displayed in a text area box at the bottom of the page. This is done in this example for educational purposes—to enable you to see the changes to the cookie as they occur; in an actual “production” page, you probably wouldn’t include it.


FIGURE 22.2  An empty Favorites list doesn’t yield a very exciting Web page.


FIGURE 22.3  The Select Favorites page displays all the possible sites as check boxes and enables the user to select and deselect which to use as favorites.


FIGURE 22.4  By enabling users to personalize their copy of your Web page, you allow a more personal experience without a greater burden on your server.

The program creates objects called “favorites.” Each favorite is, in essence, a Web link to another page. The favorite contains information on the link’s URL, a user-friendly page description, and the code that identifies it in the Favorites cookie string. The favorite also knows how to print itself on a Web page as a regular link for the Favorites page or in a check box format for the Options page. The functions used to manipulate the cookies in the FavList.htm example were shown in Listings 22.1 through 22.5. The other functions used in this example are summarized in the following list (the full HTML source code for this example is available on the CD-ROM).

  SendOptionsPage—Loads the Web browser with a page that enables the user to select which sites to be included as favorites.
  SendPersonalPage—Loads the Web browser with a page that shows either the user’s favorites or all the sites as hypertext links.
  WriteAsCheckBox—Used by SendOptionsPage to display each potential favorite site as a check box, to enable the user to select or deselect it.
  WriteAsWebLink—Used by SendPersonalPage to display each site as a hypertext link.
  LoadOptions—This function is called to initiate the display of the options page.
  ToggleView—This function is called to toggle the personal page between displaying favorites and all sites.
  favorite—This function is used to create a JavaScript object that is used to store the information used to define a favorite site.
  Enabled—This JavaScript function is used as a method by the favorite object; it returns true if the link corresponding to this object is enabled.
  Checked— This JavaScript function is used as a method by the favorite object; it returns the string CHECKED if the link corresponding to this object is enabled.
  isEnabled—Returns true if the favorite identified by the Name parameter passed to the function is enabled.
  AddFavorite—Enables the favorite identified by the Name parameter passed to the function.
  ClearFavorite—Disables the favorite identified by the Name parameter passed to the function.
  SetFavoriteEnabled—Enables or disables the favorite identified by the Name parameter passed to the function by calling AddFavorite or ClearFavorite.
  ReloadPage—Reloads the Web browser with the current page; what is displayed, however, will change according to the current state of the Document cookie.

Where Are Cookies Going?

As mentioned earlier, cookies were designed and first implemented by Netscape. However, the Internet Engineering Task Force (IETF) has a committee—the Hypertext Transfer Protocol (HTTP) Working Group—whose charter is to examine, document, and suggest ways to improve HTTP.

ON THE WEB
You can find a link to the HTTP Working Group’s latest Internet Draft, called “Proposed HTTP State Management Mechanism,” at http://www.ietf.cnri.reston.va.us/html.charters/http-charter.html.

Although the draft specification resembles Netscape cookies in theory, if not in syntax, it does have a few notable differences. It doesn’t encourage having cookies around much longer than the browser session. If the new specification is accepted, cookies are given a Max-Age lifetime rather than an expires date. All cookies expire when their time comes; but in all cases, they go away when the browser shuts down.

Reading the specification provides insight into the complexities that surround the inner workings of cookies; it is well worth the read, regardless of whether the specification is approved.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.