REV 4: December 2004
Using The Library: The library includes three core subroutines for setting, retrieving, editing and deleting client cookies:
SetCookie() sets or updates the properties of a cookie name->value pair. It takes two mandatory parameters (name and value) and four optional paramaters (expires, domain, path and/or secure).
The required arguments are name and value where name is a text string label to which a value will be assigned; and value is some value the designer wants to store and later retrieve: A name should be a quoted string of english letter, number and underscore characters. A value can be a quoted string, a numeric literal or a variable reference.
SetCookie('tempuser','john'); SetCookie('myStyle',1); SetCookie('total',document.forms.total.value);
Note that cookies set using only the name and value properties will, among other things, expire when the user ends their current session. In addition, these cookies will be accessible to other scripts and files in the same domain and will function over both secure and insecure connections.
These default behaviors can be modified by adding one or more of the optional parameters: expires, domain, path and/or secure when calling SetCookie(). Optional parameters are added left to right in the parenthesis of the call. Any overstepped arguments must be represented by a null string:
SetCookie('tempuser','john',''); SetCookie('tempuser','john',new Date());
In addition the libary includes a utility function DaysFromNow(n) that returns a localtime date value n days (or n 24 hour periods) from the current local time.
SetCookie('tempuser','john',DaysFromNow(2)); // expires in 2 days SetCookie('tempuser','john',DaysFromNow(30)); // expires in 30 days
path: The optional parameter path is a path from the top level of the current host to which the cookie's access will be restricted. The following would create (or update) a cookie restricted to files and applications found in the users/ sub-path at host:
domain: The optional parameter domain specifies a domain and/or connection method to which the cookie will be restricted.
For most purposes, it is best simply to omit domain. The default value is the current host using any method. Connection method is self limiting, and default settings in most modern browsers will resist exchanging cookies with a second designated host.
secure: The optional parameter secure is a boolean value that if true indicates that a cookie is only to accessible over a secure (https://) connection
SetCookie('tempuser','john','','','',true); // secure connections only
GetCookie() retrieves the value of value previously stored in a cookie name->value pair. GetCookie() takes one mandatory parameter name which is the quoted string label to which the value was assigned when setting the cookie:
GetCookie('name') SetCookie('tempuser','john'); var tempuser=GetCookie('tempuser'); SetCookie('myStyle',2); var activestyle=styles[((GetCookie('myStyle'))?GetCookie('myStyle'):0)];
KillCookie() permanently deletes a cookie name->value pair. It takes one parameter name which is the quoted string label to which the value was assigned:
KillCookie('name') SetCookie('tempuser','john'); KillCookie('tempuser'); SetCookie('myStyle',2); KillCookie('myStyle');
< CODEBASE | TOP^ | MAINPAGE >
Text & Design By Tony Pisarra
© SophiaKnows 1998-2004