<!-- SophiaKnows --><!-- SophiaKnows -->

REV 4: December 2004


  • cookies.js (4k)
  • cookies.html (4k)
  • cookies.zip (4k)
  • In General: This library collects a core set of client side HTTP cookies methods and fungible subroutines for manipulating cookie values and properties in a client browser.

    The library subroutines can generate and retrieve unique (i.e. javascript only) cookie key value pairs. The library subroutines can also interact, as otherwise permitted, with cookies set by a server within the same domain and/or path as the file hosting the requesting script.

    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;
  • GetCookie() retrieves the current value stored in a named cookie; and
  • KillCookie() permanently deletes a cookie name->value pair.
  • 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.


    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:


    expires: The optional parameter expires is a UTC formatted date value representing a future date on which the cookie will expire. This can be a properly formatted quoted string literal or a reference to or instance of the JavaScript date object.

    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:

    var tempuser=GetCookie('tempuser');
    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:



    Text & Design By Tony Pisarra
    © SophiaKnows 1998-2004