Java Cookies

[Java] Using JavaScript and Cookies


Here is some code I found to create cookies using Javascript. This code uses Persistent Client State HTTP Cookies to store information about how many times the browser has visited this page and when was the last time the broswer loaded the page. This example stores the two cookies on the reader's hard disk including how many times the page has been viewed by this browser and the date of the last visit.
Here is an example of how this can be used:

Hello, !

Reload this page to see this change.


How to use this Javascript:
Include the Javascripting included in the head of this document.
Make sure you rename the constants 'my_LastVisit' and 'my_NumVisits' in Javascript are something unique for your page. If you do not then other pages that use this code will interfere with your count and date.

To output the text, use this code inside the BODY section to display the information in the browser:

// Cookie Functions - Second Helping (21-Jan-96) // Written by: Bill Dortch, hIdaho Design <BDORTCH@NETW.COM> // The following functions are released to the public domain. // // The Second Helping version of the cookie functions dispenses with // my encode and decode functions, in favor of JavaScript's new built-in // escape and unescape functions, which do more complete encoding, and // which are probably much faster. // // The new version also extends the SetCookie function, though in // a backward-compatible manner, so if you used the First Helping of // cookie functions as they were written, you will not need to change any // code, unless you want to take advantage of the new capabilities. // // The following changes were made to SetCookie: // // 1. The expires parameter is now optional - that is, you can omit // it instead of passing it null to expire the cookie at the end // of the current session. // // 2. An optional path parameter has been added. // // 3. An optional domain parameter has been added. // // 4. An optional secure parameter has been added. // // For information on the significance of these parameters, and // and on cookies in general, please refer to the official cookie // spec, at: // // http://www.netscape.com/newsref/std/cookie_spec.html // //