Web design portfolio - 351Chapter 18 .The Document and Body Objects named
Monday, December 31st, 2007351Chapter 18 .The Document and Body Objects named cookie entries for any domain; IE3 imposes an even more restrictive limit of one cookie (that is, one name/value pair) per domain. If your cookie requirements are extensive, then you need to fashion ways of concatenating cookie data (I do this in the Decision Helper application in Chapter 55). Saving cookies To write cookie data to the cookie file, you use a simple JavaScript assignment operator with the document.cookieproperty. But the formatting of the data is crucial to achieving success. Here is the syntax for assigning a value to a cookie (optional items are in brackets): document.cookie = cookieName=cookieData [; expires=timeInGMTString] [; path=pathName] [; domain=domainName] [; secure] Examine each of the properties individually. Name/Data Each cookie must have a name and a string value (even if that value is an empty string). Such name/value pairs are fairly common in HTML, but they look odd in an assignment statement. For example, if you want to save the string Fred to a cookie named userName, the JavaScript statement is document.cookie = userName=Fred If the browser sees no existing cookie in the current domain with this name, it automatically creates the cookie entry for you; if the named cookie already exists, the browser replaces the old data with the new data. Retrieving document.cookie at this point yields the following string: userName=Fred You can omit all the other cookie-setting properties, in which case the browser uses default values, as explained in a following section. For temporary cookies (those that don t have to persist beyond the current browser session), the name/value pair is usually all you need. The entire name/value pair must be a single string with no semicolons, commas, or character spaces. To take care of spaces between words, preprocess the value with the JavaScript escape() function, which URL-encodes the spaces as %20(and then be sure to unescape() the value to restore the human-readable spaces when you retrieve the cookie later). You cannot save a JavaScript array or object to a cookie. But with the help of the Array.join() method, you can convert an array to a string; use String.split() to re-create the array after reading the cookie at a later time. These two methods are available in NN3+ and IE4+. Expires Expiration dates, when supplied, must be passed as Greenwich Mean Time (GMT) strings (see Chapter 36 about time data). To calculate an expiration date based on today s date, use the JavaScript Date object as follows: document.cookie
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.