The cookie is a mechanism for storing persistent data on the client.  Because HyperText Transport Protocol (HTTP) is a stateless protocol, cookies are used to provide a way to maintain information between client requests.  You may store a maximum of 300 total cookies in the cookie file, at up to 4 Kbytes per cookie for the sum of both the cookie's name and value, and no more than 20 cookies per server or domain.  Full documentation for cookies can be found in Netscape's JavaScript Guide



Standard Cookies:

The simplest cookie has a name to use for refering to it, a value that you may retrieve later, and an expiration date after which it will be erased.  The subroutines to use for getting and setting such cookies are put into the <head> section of the page and may look like this:

 
<script type="text/javascript">
  <!-- Hide this code from non-JavaScript browsers
    function GetCookie(name) {
      c1 = document.cookie;
      c1 = c1.replace(/\n/g, "");               // drop nl chars
      c1 = c1.replace(/\r/g, "");               // drop cr chars
      ix1 = 0;                                  // from beginning
      c1 = c1 + ";";                            // for final cookie
      while (c1.indexOf(";", ix1) != -1) {
        ix2 = c1.indexOf(";", ix1);             // next cookie break
        if (ix2 == -1) { ix2 = c1.length; }
        c2 = c1.substring(ix1, ix2);            // a single cookie
        ix1 = ix2 + 1;
        ix3 = c2.indexOf("=", 0);               // name=value break
        if (ix3 == -1) { ix3 = c2.length; }
        c3 = c2.substring(0, ix3);              // get the name
        c4 = c2.substring(ix3 + 1);             // get the value
        c3 = c3.replace(/ /g, "");              // drop all spaces
        if (c3 == name) { return unescape(c4); }
      }
      return true;
    }
    function SetCookie(name, value, expires, days) {
      p3 = expires;
      p4 = days;
      if (!p3) {                                // no expiration
        if (!p4) { p4 = 1; }                    // no days: use 1
        p3 = new Date();
        msd = 1000 * 60 * 60 * 24;              // millisecs per day
        p3.setTime(p3.getTime() + msd * days);
      }
      document.cookie=name+"="+escape(value)+"; expires="+p3.toGMTString();
      return true;
    }
  // End hiding -->
</script>

The code to get and set cookies goes within the <body> at the point where you want them displayed.  It might look like this:

 
<script type="text/javascript">
  <!-- Hide this code from non-JavaScript browsers
    lvdate = GetCookie("lvdate");
    if (lvdate) {                            
      xeq  = "<p>";
      xeq += "Your last visit was " + lvdate + ". ";
      xeq += "It's nice to see you again.";
      xeq += "</p>";
      document.write(xeq);
    }
    lvdate = new Date();
    SetCookie("lvdate", lvdate, "", 1);
  // End hiding -->
</script>

Top / Standard Cookies / Subdivided Cookies / Try it / Display Cookies

Subivided Cookies:

It is possible to set a cookie's value to multiple name/value pairs to store multiple items per cookie.  Subroutines to do this get a bit more complex than the standard ones listed above, and may look like this:

 
<script type="text/javascript">
  <!-- Hide this code from non-JavaScript browsers
    function GetCookie(name) {
      var cookies = document.cookie;            // cookies for site
      var ix1;                                  // loop index
      var n1 = name;                            // main cookie name
      var n2;                                   // crumb name
      var out;                                  // returned value
      ix1 = n1.indexOf(";");                    // cookie/crumb break
      if (ix1 != -1) {
        n2 = n1.substring(ix1 + 1);             // crumb name
        n1 = n1.substring(0, ix1);              // main cookie name
      }
      out = GetCookieCrumb(cookies, n1);
      if (n2 && out) { out = GetCookieCrumb(out, n2); }
      cookie = rest;
      return out;
    }
    function SetCookie(name, value, expires, days) {
      var cookies = document.cookie;            // cookies for site
      var ix1;                                  // index
      var n1 = name;                            // main cookie name
      var n2 = "";                              // crumb name
      var out;                                  // returned value
      var p2 = value;                           // new value
      var p3 = expires;                         // param work area
      var p4 = days;
      var msd = 1000 * 60 * 60 * 24;            // millisecs per day
      if (!p3) {                                // no expiration
        if (!p4) { p4 = 1; }                    // no days: use 1
        p3 = new Date();
        p3.setTime(p3.getTime() + msd * days);
      }
      ix1 = n1.indexOf(";");                    // cookie/crumb break
      if (ix1 != -1) {
        n2 = n1.substring(ix1 + 1);             // crumb name
        n1 = n1.substring(0, ix1);              // main cookie name
      }
      out = GetCookieCrumb(cookies, n1);
      if (n2) {
        if (out) { out = GetCookieCrumb(out, n2); }
        else { rest = ""; }
        if (rest) { p2 = rest + ";" + n2 + "=" + p2; }
        else      { p2 =              n2 + "=" + p2; }
      }
      document.cookie=n1+"="+escape(p2)+"; expires="+p3.toGMTString();
      return true;
    }
    function GetCookieCrumb(instring, seeking) {
      var c1;                                   // cookies work area
      var c2;                                   // cookie work area
      var c3;                                   // cookie name
      var c4;                                   // cookie value
      var ix1; var ix2; var ix3; var ix4;       // indexes
      var out;                                  // return value
      rest = "";          
      c1 = instring;
      c1 = c1.replace(/\n/g, "");               // drop nl chars
      c1 = c1.replace(/\r/g, "");               // drop cr chars
      c1 = c1 + ";";                            // for final cookie
      ix1 = 0;                                  // from beginning
      if (seeking.substring(0, 1) == "#") {
        if (seeking == "#0" && !instring) { return 0; }
        ix4 = seeking.substring(1);             // which item wanten
        ix3 = 0;                                // none so far
        while (c1.indexOf(";", ix1) != -1) {
          ix2 = c1.indexOf(";", ix1);
          cookie = c1.substring(ix1, ix2);
          ix3 += 1;
          if (ix3 == ix4) { return cookie; }
          ix1 = ix2 + 1;
        }
        if (ix4 == 0) { return ix3; }           // count of items
        return "";
      }
      while (c1.indexOf(";", ix1) != -1) {
        ix2 = c1.indexOf(";", ix1);             // next cookie break
        if (ix2 == -1) { ix2 = c1.length; }
        cookie = c1.substring(ix1, ix2);        // a single cookie
        c2 = cookie;                            // cookie work area
        ix1 = ix2 + 1;
        ix3 = c2.indexOf("=", 0);               // name=value break
        if (ix3 == -1) { ix3 = c2.length; }
        c3 = c2.substring(0, ix3);              // get the name
        c4 = c2.substring(ix3 + 1);             // get the value
        c3 = c3.replace(/ /g, "");              // drop all space
        if (c3 == seeking) {
          out = unescape(c4);
        } else {
          if (rest) { rest = rest + ";" + cookie; }
          else      { rest = cookie; }
        }
      }
      return out;
    }
  // End hiding -->
</script>

To make use of a subdivided cookie, put a semicolon (;) after the main cookie name and then add the sub-name like this:

 
<script type="text/javascript">
  <!-- Hide this code from non-JavaScript browsers
    p_name = GetCookie("personal;name");
    if (p_name) {
      xeq  = "<p>";
      xeq += "I remember you, " + p_name + ".&nbsp;";
      p_adr = GetCookie("personal;address");
      xeq += "You live at " + p_adr + ".&nbsp;";
      xeq += "</p>";
      document.write(xeq);
    }
    SetCookie("personal;name", "Casey Jones", "", 10);
    SetCookie("personal;address", "2 2 Twain St, Railway Station,Texas", "", 10);
  // End hiding -->
</script>

Top / Standard Cookies / Subdivided Cookies / Try it / Display Cookies

Try it:


days.
   

You may set up your favorite links to automatically appear on this page for the next three months:

ex: James Alarie Home Page
ex: http://spruce.flint.umich.edu/~jalarie/
 

Top / Standard Cookies / Subdivided Cookies / Try it / Display Cookies

Display Cookies:

If you are running a Windows 95/98 machine using Netscape in its default configuration, your cookies are probably stored in c:/Program%20Files/Netscape/Users/default/cookies.txt.  It may not be a display of all current cookies because Netscape may still be holding some in memory.  You would have to shut down the browser and start it again to be certain of an up-to-date file.  To get rid of Netscape cookies stored on your machine, select "Edit > Preferences > Advanced," choose "Disable cookies," select "OK," and then shut down your browser.  Do not attempt to edit the file directly.

On a Windows 95/98 machine set up for a single user, Internet Explorer is probably storing its cookies in the c:/Windows/Cookies/ directory.  If you wish to get rid of IE cookies, use Internet Explorer and select "Tools > Internet Options > Settings > View Files," look down the list under "Internet Address" looking for anything that begins with "Cookie:," select the entry, and then push the "delete" key.  Repeat the procedure for any other cookies on the list and then get back out of the drop-down boxes.

Top / Standard Cookies / Subdivided Cookies / Try it / Display Cookies