asp.net.ph

Skip Navigation Links

cookie Property

document Properties   Object Properties Summary


Sets or retrieves the cookies associated with the document.

Syntax


HTML N/A
Script document.cookie [ = "name=value; [ expires=date; domain=domainname; path=path; secure; ] " ]

Possible Values


name=value; Each cookie is stored in a name=value pair — that is, if the cookie is "id" and you want to save id's value as "this", the cookie would be saved as id=this. You can save as many name=value pairs in the cookie as you want, but the cookie is always returned as a string of all the cookies that apply to the page. So the string that is returned has to be parsed to find the values of individual cookies.
expires=date; If no expiration is set on a cookie, it will expire when the browser is closed. Setting an expiration date in the future causes the cookie to be saved across browser sessions. Setting an expiration date in the past will delete a cookie. The date should be specified using GMT format.
domain=domainname; The domain of the cookie can be set, which allows pages on a domain made up of more than one server to share cookie information.
path=path; Setting a path for the cookie will allow the current document to share cookie information with other pages within the same domain—that is, if the path is set to /thispathName, all pages in /thispathName and all pages in subfolders of /thispathName will be able to access the same cookie information.
secure; Specifying a cookie as "secure" means that the stored cookie information can be accessed only from a secure environment.

The property is read/write with no default value.

Remarks

This property returns the string value of a cookie, which is a small piece of information stored by the browser.

If there are none, the value is an empty string. Otherwise, the value is a string: a semicolon-delimited list of "name, value" pairs for all the cookies associated with the page. For example, name=value; expires=date.

The JScript® ( compatible with ECMA-262 language specification ) split method can be used to extract a value stored in a cookie.

Example

The following example shows a function that takes at least two arguments, the cookie name and the cookie value, and four optional arguments, in this order: an expires Date object, a string path, a string domain, and a boolean value secure ( for HTTPS cookies only ) :

The value is passed to the ECMA escape function to ensure that the value contains only legal characters. When the cookie is retrieved, the ECMA unescape function should be used to translate the value back to its original form.

<script language="JavaScript">
function setCookie ( name, value, expires ) {
document.cookie = name + "="+ escape ( value ) + ";
  + ( expires ? '; EXPIRES=' + expires.toGMTString ( ) : '' )
  + ( path ? '; PATH=' + path : '' )
  + ( domain ? '; DOMAIN=' + domain : '' )
  + ( secure ? '; SECURE' : '' );";
}
</script>

Examples of use:

// set a session cookie which expires after the browser is closed
setCookie ( 'cookieName', 'cookieValue' );

// set a cookie which expires after 24 hours
var now = new Date ( );
var tomorrow = new Date ( now.getTime ( ) + 
  1000 * 60 * 60 * 24 )
setCookie ( 'cookieName', 'cookieValue', tomorrow );

// set a cookie with a path
setCookie ( 'cookieName', 'cookieValue', null, '/' ) 

The following example shows how to retrieve a cookie with a specified name.

function getCookie ( cookieName ) {
var cookieValue = null;
var posName = document.cookie.indexOf ( escape ( cookieName )
  + '=' );
if ( posName != -1 ) {
  var posValue = posName + ( escape ( cookieName )
    + '=' ).length;
  var endPos = document.cookie.indexOf ( ';', posValue );
  if ( endPos != -1 )
    cookieValue = unescape ( document.cookie.substring ( 
      posValue, endPos ) );
  else
    cookieValue = unescape ( document.cookie.substring ( 
      posValue ) );
  }
return cookieValue;
}

The sample below shows how to delete a cookie by setting an expiration date in the past.

var now = new Date ( );
var yesterday = new Date ( now.getTime ( ) - 
  1000 * 60 * 60 * 24 );
setCookie ( 'cookieName', 'cookieValue', yesterday );
Applies To

document



>

© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note