Click Here!
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


Of course, if a GetCookieCount function exists, a GetCookieNum function should be available to retrieve a particular instance of a cookie. That function would look like this:

function GetCookieNum(name,cookieNum) {
   var result = null;
   if (cookieNum >= 1) {
      var myCookie = “ ” + document.cookie + “;”;
      var searchName = “ ” + name + “=”;
      var nameLength = searchName.length;
      var startOfCookie = myCookie.indexOf(searchName);
      var cntr = 0;
      for (cntr = 1; cntr < cookieNum; cntr++)
         startOfCookie = myCookie.indexOf(searchName,startOfCookie + nameLength);
      if (startOfCookie != -1) {
         startOfCookie += nameLength; // skip past cookie name
         var endOfCookie = myCookie.indexOf(“;”,startOfCookie);
         result = unescape(myCookie.substring(startOfCookie,endOfCookie));
      }
   }
   return result;
}


CAUTION:  

A bug is present in Netscape Navigator version 1.1 and earlier. Only cookies whose Path attribute is set explicitly to / are properly saved between sessions if they have an Expires attribute.


To delete a cookie, the Name and the Path must match the original Name and Path used when the cookie was set.

Domainm Usually, after a page on a particular server creates a cookie, that cookie is accessible only to other pages on that server. Just as the Path parameter makes a cookie available outside its home path, the Domain parameter makes it available to other Web servers at the same site.

You can’t create a cookie that anyone on the Internet can see. You may only set a Path that falls inside your own Domain. This is because the use of the Domain parameter dictates that you must use at least two periods (for example, .mydomain.com) if your domain ends in .com, .edu, .net, .org, .gov, .mil, or .int. Otherwise, it must have at least three periods (.mydomain.ma.us). Your Domain parameter string must match the tail of your server’s domain name.

Secure The final cookie parameter tells your browser that this cookie should be sent only under a Secure connection with the Web server. This means that the server and the browser must support HTTPS security. (HTTPS is Netscape’s Secure Socket Layer Web page encryption protocol.)

If the Secure parameter is not present, it means that cookies are sent unencrypted over the network.


NOTE:  You can’t set an infinite number of cookies on every Web browser that visits your site. The following list shows the number of cookies you can set and how large they can be:
  Cookies per each server or domain: 20
  Total cookies per browser: 300
  Largest cookie: 4KB (including both the Name and Value parameters)

If these limits are exceeded, the browser might attempt to discard older cookies by tossing out the least recently used cookies first.


Now that you have seen all the cookie parameters, it would be helpful to have a JavaScript routine set cookies with all the parameters. Such a routine might look like that shown in Listing 22.4.

Listing 22.4 FavList.htm (excerpt)—JavaScript Routine to Add a Cookie, Including Any Optional Parameters


// SetCookie - Adds or replaces a cookie. Use null for parameters
//             that you don’t care about
//
function SetCookie(name,value,expires,path,domain,secure) {
   var expString =
      ((expires == null) ? “” : (“; expires=” + expires.toGMTString()))
   var pathString = ((path == null) ? “” : (“; path=” + path))
   var domainString =
      ((domain == null) ? “” : (“; domain=” + domain))
   var secureString = ((secure == true) ? “; secure” : “”)
   document.cookie = name + “=” + escape(value) +
                     expString + pathString + domainString +
                     secureString;
}

To use this routine, you call it with whatever parameters you care about and use null in place of parameters that don’t matter.

A Cookie Example

The JavaScript program in this example is in the HTML document FavList.htm, which is included on the CD-ROM that comes with this book. Excerpts of the program were shown in Listings 22.1 through 22.4; these showed the JavaScript functions used to create and manipulate the document cookies used in this example. Listing 22.5 shows the actual <BODY> section of the FavList.htm example, which enables the user to create a personalized “News-of-the-Day” page containing links to sites of general interest in a number of categories. The user’s favorite links are stored in cookies.

Listing 22.5 FavList.htm (excerpt)—The <BODY> Section of the Cookie Example


<BODY BGCOLOR=#FFFFFF>
<SCRIPT LANGUAGE=“JavaScript”>
<!-- Hide script from incompatible browsers!
//
// Here’s where we select the page to send. Normally we send the
// personalized favorites page (by calling SendPersonalPage). However,
// If the cookie ShowOptions is set, we’ll send the options selection
// page instead (by calling SendOptionsPage).
//
if (GetCookie(“ShowOptions”) == “T”) {
   ClearCookie(“ShowOptions”);
   SendOptionsPage();
} else
   SendPersonalPage();
//   Hide script from incompatible browsers! -->
</SCRIPT>
<HR>
<H2>Current Document Cookie Contents...</H2>
<CENTER>
<FORM NAME=“MyForm”>
<TEXTAREA NAME=“MyTextArea” ROWS=1 COLS=60>
</TEXTAREA>
</FORM>
</CENTER>
<SCRIPT LANGUAGE=“JavaScript”>
<!-- Hide script from incompatible browsers!
document.MyForm.MyTextArea.value = document.cookie;
//   Hide script from incompatible browsers! -->
</SCRIPT>
<HR>
<ADDRESS>
Jim O’Donnell, <A HREF=“mailto:odonnj@rpi.edu”>odonnj@rpi.edu</A>
</ADDRESS>
</BODY>
</HTML>

As shown in Listing 22.5, when this page is loaded, one of two JavaScripts will be called to actually “fill” the page: either SendOptionsPage() or SendPersonalPage(). The former enables the user to select from a list of sites to be included as favorites; the latter is used to display those sites (or to display all the possible sites). Figure 22.1 shows this page when it is first loaded, before the user has selected a list of favorites (so all possible sites are shown).


FIGURE 22.1  The Favorites page displays all possible sites when first loaded.


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.