cookieLibrary.js 写入cookie的JavaScript代码库

js code:

  1/* Cookie Library -- "Night of the Living Cookie" Version (25-Jul-96)
  2缔友计算机信息技术有限公司,涂聚文  geovindu@163.com 互相交流
  3  Written by:  Bill Dortch, hIdaho Design <geovindu@163.com>
  4  The following functions are released to the public domain.
  5http://www.dusystem.com/
  6  This version takes a more aggressive approach to deleting
  7  cookies.  Previous versions set the expiration date to one
  8  millisecond prior to the current time; however, this method
  9  did not work in Netscape 2.02 (though it does in earlier and
 10  later versions), resulting in "zombie" cookies that would not
 11  die.  DeleteCookie now sets the expiration date to the earliest
 12  usable date (one second into 1970), and sets the cookie's value
 13  to null for good measure.
 14
 15  Also, this version adds optional path and domain parameters to
 16  the DeleteCookie function.  If you specify a path and/or domain
 17  when creating (setting) a cookie**, you must specify the same
 18  path/domain when deleting it, or deletion will not occur.
 19
 20  The FixCookieDate function must now be called explicitly to
 21  correct for the 2.x Mac date bug.  This function should be
 22  called *once* after a Date object is created and before it
 23  is passed (as an expiration date) to SetCookie.  Because the
 24  Mac date bug affects all dates, not just those passed to
 25  SetCookie, you might want to make it a habit to call
 26  FixCookieDate any time you create a new Date object:
 27
 28    var theDate = new Date();
 29    FixCookieDate (theDate);
 30
 31  Calling FixCookieDate has no effect on platforms other than
 32  the Mac, so there is no need to determine the user's platform
 33  prior to calling it.
 34
 35  This version also incorporates several minor coding improvements.
 36
 37  **Note that it is possible to set multiple cookies with the same
 38  name but different (nested) paths.  For example:
 39
 40    SetCookie ("color","red",null,"/outer");
 41    SetCookie ("color","blue",null,"/outer/inner");
 42
 43  However, GetCookie cannot distinguish between these and will return
 44  the first cookie that matches a given name.  It is therefore
 45  recommended that you *not* use the same name for cookies with
 46  different paths.  (Bear in mind that there is *always* a path
 47  associated with a cookie; if you don't explicitly specify one,
 48  the path of the setting document is used.)
 49  
 50  Revision History:
 51
 52    "Toss Your Cookies" Version (22-Mar-96)
 53      - Added FixCookieDate() function to correct for Mac date bug
 54
 55    "Second Helping" Version (21-Jan-96)
 56      - Added path, domain and secure parameters to SetCookie
 57      - Replaced home-rolled encode/decode functions with Netscape's
 58        new (then) escape and unescape functions
 59
 60    "Free Cookies" Version (December 95)
 61
 62
 63  For information on the significance of cookie parameters, and
 64  and on cookies in general, please refer to the official cookie
 65  spec, at:
 66
 67      http:www.netscape.com/newsref/std/cookie_spec.html    
 68
 69****************************************************************** */

 70
 71/* "Internal" function to return the decoded value of a cookie */
 72function getCookieVal (offset) {
 73    var endstr = document.cookie.indexOf (";", offset);
 74    if (endstr == -1{
 75      endstr = document.cookie.length;
 76    }

 77    return unescape(document.cookie.substring(offset, endstr));
 78}

 79
 80/* Function to correct for 2.x Mac date bug.  Call this function to
 81    fix a date object prior to passing it to SetCookie.
 82       IMPORTANT:  This function should only be called *once* for
 83       any given date object!  See example at the end of this document. */

 84function FixCookieDate (date) {
 85    var base = new Date(0);
 86    var skew = base.getTime(); // dawn of (Unix) time - should be 0
 87    if (skew > 0{              // except on the Mac - ahead of its time
 88        date.setTime(date.getTime() - skew);
 89    }

 90}

 91
 92/* Function to return the value of the cookie specified by "name".
 93    name - String object containing the cookie name.
 94    returns - String object containing the cookie value, or null if
 95    the cookie does not exist. */

 96function GetCookie (name) {
 97    var temp = name + "=";
 98    var tempLen = temp.length;
 99    var cookieLen = document.cookie.length;
100    var i = 0;
101    while (i < cookieLen) {
102        var j = i + tempLen;
103        if (document.cookie.substring(i, j) == temp) {
104            return getCookieVal(j);
105        }

106           i = document.cookie.indexOf(" ", i) + 1;
107           if (i == 0break
108    }

109    return null;
110}

111
112/*  Function to create or update a cookie.
113    name - String object containing the cookie name.
114    value - String object containing the cookie value.  May contain
115      any valid string characters.
116    [expiresDate] - Date object containing the expiration data of the cookie.  If
117      omitted or null, expires the cookie at the end of the current session.
118    [path] - String object indicating the path for which the cookie is valid.
119      If omitted or null, uses the path of the calling document.
120    [domain] - String object indicating the domain for which the cookie is
121      valid.  If omitted or null, uses the domain of the calling document.
122    [secure] - Boolean (true/false) value indicating whether cookie transmission
123      requires a secure channel (HTTPS).  
124
125    The first two parameters are required.  The others, if supplied, must
126    be passed in the order listed above.  To omit an unused optional field,
127    use null as a place holder.  For example, to call SetCookie using name,
128    value and path, you would code:
129    
130        SetCookie ("myCookieName", "myCookieValue", null, "/");
131    
132    Note that trailing omitted parameters do not require a placeholder.
133    
134    To set a secure cookie for path "/myPath", that expires after the
135    current session, you might code:
136
137        SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true); */

138function SetCookie (name,value,expiresDate,path,domain,secure) {
139    document.cookie = name + "=" + escape (value) +
140        ((expiresDate) ? "; expires=" + expiresDate.toGMTString() : ""+
141        ((path) ? "; path=" + path : ""+
142        ((domain) ? "; domain=" + domain : ""+
143        ((secure) ? "; secure" : "");
144}

145
146/*  Function to delete a cookie. (Sets expiration date to start of epoch)
147    name -   String object containing the cookie name
148    path -   String object containing the path of the cookie to delete.  This MUST
149             be the same as the path used to create the cookie, or null/omitted if
150             no path was specified when creating the cookie.
151    domain - String object containing the domain of the cookie to delete.  This MUST
152             be the same as the domain used to create the cookie, or null/omitted if
153             no domain was specified when creating the cookie. */

154function DeleteCookie (name,path,domain) {
155    if (GetCookie(name)) {
156        document.cookie = name + "=" +
157            ((path) ? "; path=" + path : ""+
158            ((domain) ? "; domain=" + domain : ""+
159            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
160    }

161}

162
163
164// Calling examples:
165//    var expdate = new Date ();
166//    FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
167//    expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
168//    SetCookie ("ccpath", "http://www.dupcit.com/articles/", expdate);
169//    SetCookie ("ccname", "WebWoman", expdate);
170//    SetCookie ("tempvar", "This is a temporary cookie.");
171//    SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
172//    SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
173//    SetCookie ("goner", "This cookie must die!");
174//    document.write (document.cookie + "<br>");
175//    DeleteCookie ("goner");
176//    document.write (document.cookie + "<br>");
177//    document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
178//    document.write ("ccname = " + GetCookie("ccname") + "<br>");
179//    document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
180
181

 

posted @ 2009-10-23 19:15  ®Geovin Du Dream Park™  阅读(503)  评论(0编辑  收藏  举报