cookie Property--------------------------------------------------------------------------------Sets or retrieves the string value of a cookie.SyntaxHTML N/A 
Scripting document.cookie [ = sCookie ] Possible ValuessCookie String that specifies or receives the name=value; pairs, plus any of the values listed in Possible Values. expires=date; Setting no expiration date on a cookie causes it to expire when the browser closes. If you set an expiration date in the future, the cookie is saved across browser sessions. If you set an expiration date in the past, the cookie is deleted. Use GMT format to specify the date.  
domain=domainname; Setting the domain of the cookie allows pages on a domain made up of more than one server to share cookie information.  
path=path; Setting a path for the cookie allows 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 can access the same cookie information.  
secure; Setting a cookie as secure; means the stored cookie information can be accessed only from a secure environment. 
 The property is read/write. The property has no default value.Expressions can be used in place of the preceding value(s), as of Microsoft&reg; Internet Explorer 5. For more information, see Dynamic Properties.ResA cookie is a small piece of information stored by the browser. 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's value as "this", the cookie would be saved as id=this. You can store up to 20 name=value pairs in a cookie, and the cookie is always returned as a string of all the cookies that apply to the page. This means that you must parse the string returned to find the values of individual cookies.Cookies accumulate each time the property is set. If you try to set more than one cookie with a single call to the property, only the first cookie in the list will be retained.You can use the Microsoft&reg; JScript&reg; split method to extract a value stored in a cookie. ExamplesThis example creates a cookie with a specified name and value. The value is passed to the JScript escape function to ensure that the value only contains valid characters. When the cookie is retrieved, the JScript unescape function should be used to translate the value back to its original form.<SCRIPT>
// Create a cookie with the specified name and value.
// The cookie expires at the end of the 20th century.
function SetCookie(sName, sValue)
{
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}
</SCRIPT>
This example retrieves the value of the portion of the cookie specified by the sCookie parameter.Show Example
<SCRIPT>
// 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;
}
</SCRIPT>
This example deletes a cookie by setting its expires attribute to a past date. A cookie deleted in this manner might not be removed immediately by the browser.<SCRIPT>
// Delete the cookie with the specified name.
function DelCookie(sName)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
</SCRIPT>

解决方案 »

  1.   

    http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp
      

  2.   

    http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm#1002170
      

  3.   


    Using Cookies
    Netscape cookies are a mechanism for storing persistent data on the client in a file called cookies.txt. Because HyperText Transport Protocol (HTTP) is a stateless protocol, cookies provide a way to maintain information between client requests. This section discusses basic uses of cookies and illustrates with a simple example. For a complete description of cookies, see Appendix C, "Netscape Cookies.".
    Each cookie is a small item of information with an optional expiration date and is added to the cookie file in the following format:
    name=value;expires=expDate;
    name is the name of the datum being stored, and value is its value. If name and value contain any semicolon, comma, or blank (space) characters, you must use the escape function to encode them and the unescape function to decode them.
    expDate is the expiration date, in GMT date format:
    Wdy, DD-Mon-YY HH:MM:SS GMT
    Although it's slightly different from this format, the date string returned by the Date method toGMTString can be used to set cookie expiration dates.
    The expiration date is an optional parameter indicating how long to maintain the cookie. If expDate is not specified, the cookie expires when the user exits the current Navigator session. Navigator maintains and retrieves a cookie only if its expiration date has not yet passed. For more information on escape and unescape, see the JavaScript Reference.
    Limitations
    Cookies have these limitations:
    300 total cookies in the cookie file. 4 Kbytes per cookie, for the sum of both the cookie's name and value. 20 cookies per server or domain (completely specified hosts and domains are treated as separate entities and have a 20-cookie limitation for each, not combined). 
    Cookies can be associated with one or more directories. If your files are all in one directory, then you need not worry about this. If your files are in multiple directories, you may need to use an additional path parameter for each cookie. For more information, see Appendix C, "Netscape Cookies."Using Cookies with JavaScript
    The document.cookie property is a string that contains all the names and values of Navigator cookies. You can use this property to work with cookies in JavaScript.
    Here are some basic things you can do with cookies:Set a cookie value, optionally specifying an expiration date. Get a cookie value, given the cookie name. 
    It is convenient to define functions to perform these tasks. Here, for example, is a function that sets cookie values and expiration:// Sets cookie values. Expiration date is optional//function setCookie(name, value, expire) {   document.cookie = name + "=" + escape(value)   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))}
    Notice the use of escape to encode special characters (semicolons, commas, spaces) in the value string. This function assumes that cookie names do not have any special characters.
    The following function returns a cookie value, given the name of the cookie:
    function getCookie(Name) {   var search = Name + "="   if (document.cookie.length > 0) { // if there are any cookies      offset = document.cookie.indexOf(search)       if (offset != -1) { // if cookie exists          offset += search.length          // set index of beginning of value         end = document.cookie.indexOf(";", offset)          // set index of end of cookie value         if (end == -1)             end = document.cookie.length         return unescape(document.cookie.substring(offset, end))      }    }}
    Notice the use of unescape to decode special characters in the cookie value.Using Cookies: an Example
    Using the cookie functions defined in the previous section, you can create a simple page users can fill in to "register" when they visit your page. If they return to your page within a year, they will see a personal greeting. 
    You need to define one additional function in the HEAD of the document. This function, register, creates a cookie with the name TheCoolJavaScriptPage and the value passed to it as an argument.
    function register(name) {   var today = new Date()   var expires = new Date()   expires.setTime(today.getTime() + 1000*60*60*24*365)   setCookie("TheCoolJavaScriptPage", name, expires)}
    The BODY of the document uses getCookie (defined in the previous section) to check whether the cookie for TheCoolJavaScriptPage exists and displays a greeting if it does. Then there is a form that calls register to add a cookie. The onClick event handler also calls history.go(0) to redraw the page.<BODY><H1>Register Your Name with the Cookie-Meister</H1><P><SCRIPT>var yourname = getCookie("TheCoolJavaScriptPage") if (yourname != null)   document.write("<P>Welcome Back, ", yourname)else   document.write("<P>You haven't been here in the last year...")</SCRIPT>
    <P> Enter your name. When you return to this page within a year, you will be greeted with a personalized greeting. <BR><FORM onSubmit="return false">Enter your name: <INPUT TYPE="text" NAME="username" SIZE= 10><BR><INPUT TYPE="button" value="Register"   onClick="register(this.form.username.value); history.go(0)"></FORM>