|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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; }
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 cant 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 servers 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 Netscapes Secure Socket Layer Web page encryption protocol.) If the Secure parameter is not present, it means that cookies are sent unencrypted over the network.
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 dont 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 dont matter. A Cookie Example
Listing 22.5 FavList.htm (excerpt)The <BODY> Section of the Cookie Example <BODY BGCOLOR=#FFFFFF> <SCRIPT LANGUAGE=JavaScript> <!-- Hide script from incompatible browsers! // // Heres where we select the page to send. Normally we send the // personalized favorites page (by calling SendPersonalPage). However, // If the cookie ShowOptions is set, well 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 ODonnell, <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).
|
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. |