13 out of 28 rated this helpful Rate this topic

cookie Property

.NET Framework 3.0

Sets or gets the string value of a cookie.

Syntax

[ sCookie = ] object.cookie

Possible Values

sCookie String that specifies or receives the name=value; pairs, plus any of the values listed in Possible Values.
expires=date;
If you set no expiration date on a cookie, it expires when the browser closes. If you set an expiration date, the cookie is saved across browser sessions. If you set an expiration date in the past, the cookie is deleted. Use Greenwich Mean Time (GMT) format to specify the date.
domain=domainname;
If you set the domain of the cookie, documents on a domain made up of more than one server can share cookie information.
path=path;
If you set a path for the cookie, the current document can share cookie information with other documents within the same domain—that is, if the path is set to /thispathname, all documents in /thispathname and all documents in subfolders of /thispathname can access the same cookie information.
secure;
If you set a cookie as secure;, the stored cookie information can be accessed only from Secure Hypertext Transfer Protocol (HTTPS).

The property is read/write. The property has no default value.

DHTML expressions can be used in place of the preceding value(s). As of Internet Explorer 8, expressions are supported in IE7 Standards mode and IE5 (Quirks) mode only. For more information, see About Dynamic Properties and Defining Document Compatibility.

Remarks

A cookie is a small piece of information. Each cookie is stored in a name=value pair called a crumb—that is, if the cookie name is "id" and you want to save the id value as "this," the cookie is saved as id=this. You can store up to a maxium of 50 name=value pairs in a cookie; the cookie is always returned as a string of all the cookies that apply to the document. This means that you must parse the string returned to find the values of individual cookies.

Cookies accumulate each time the property is set. Once the maxium pair limit is reached, subsequent set will push older name=value pair off in favor of the new name=value pair.

You can use the Microsoft JScript  split method to extract a value stored in a cookie.

Examples

The following example creates a cookie with a specified name and value. The value is passed to the JScript escape function to ensure that the value contains only valid characters. When you get the cookie, use the JScript unescape function to translate the value back to its original form.


// Create a cookie with the specified name and value.
function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue);
  // Expires the cookie in one month
  var date = new Date();
  date.setMonth(date.getMonth()+1);
  document.cookie += ("; expires=" + date.toUTCString()); 
}

This next example retrieves the value of a cookie with a specified name.


// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  // a cookie with the requested name does not exist
  return null;
}

This last example deletes a cookie by setting its expires attribute to a past date. A cookie deleted in this manner might not be removed immediately.


// Delete the cookie with the specified name.
function DelCookie(sName)
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/cookie.htm

Standards Information

This property is defined in Document Object Model (DOM) Level 1.

Applies To

document, HTMLDocument Constructor

See Also

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
More info on cookie behavior
This FAQ explains many of the behaviors of cookies in Internet Explorer that are not obvious:
"Internet Explorer Cookie Internals FAQ" in EricLaw's IEInternals blog:
http://blogs.msdn.com/ieinternals/archive/2009/08/20/WinINET-IE-Cookie-Internals-FAQ.aspx