January 9, 2002 - Differentiating between Cookies | WebReference

January 9, 2002 - Differentiating between Cookies

Yehuda Shiran January 9, 2002
Differentiating between Cookies
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

One way to store and retrieve data on the client side is by using cookies. The cookie property is a member of the document object. You can store thousands of cookies per client. You can distinguish between cookies by several parameters:

name. The name of the cookie. A quoted string. Required. Example: "listSelectedIndex".
  • value. The value of the cookie. A numeric value or a quoted string. Required. Example: 5.
  • expires. Expiration date of the cookie. A date object. Optional. Defaults to the end of the current session. Example: now.
  • path. The path for which the cookie is valid. A quoted string. Optional. Example: "/foo/bar/html". Defaults to the path of the document or script that set the cookie.
  • domain. The domain for which the cookie is valid. A quoted string. Optional. Example: "www.internet.com". The default value of the domain attribute is the host name of the server that set the cookie.
  • secure. A Boolean value indicating whether the cookie transmission requires a secure protocol. Default to false.

    To set up a date object to expire in one year, you need to multiply the following:

    365 days in a year.
  • 24 hours in a day.
  • 60 minutes in an hour.
  • 60 seconds in a minute.
  • 1000 milliseconds in a second.

    The following line of code computes the date object for the year-long cookie:

      var now = new Date();
      now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);