Cookie writing JavaScript code library cookieLibrary. js

Source: Internet
Author: User

/* Cookie Library -- "Night of the Living Cookie" Version (25-Jul-96)
2 Association Friends Computer Information Technology Co., Ltd., tu juwen geovindu@163.com exchange with each other
3 Written by: Bill Dortch, hIdaho Design <geovindu@163.com>
4 The following functions are released to the public domain.
5 http://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
Later versions), resulting in "zombie" cookies that wocould not
Die. DeleteCookie now sets the expiration date to the earliest
Usable date (one second into 1970), and sets the cookie's value
To null for good measure.

Also, this version adds optional path and domain parameters
The DeleteCookie function. If you specify a path and/or domain
When creating (setting) a cookie **, you must specify the same
Path/domain when deleting it, or deletion will not occur.

The FixCookieDate function must now be called explicitly
Correct for the 2.x Mac date bug. This function shocould be
Called * once * after a Date object is created and before it
Is passed (as an expiration date) to SetCookie. Because
Mac date bug affects all dates, not just those passed
SetCookie, you might want to make it a habit to call
FixCookieDate any time you create a new Date object:

Var theDate = new Date ();
FixCookieDate (theDate );

Calling FixCookieDate has no effect on platforms other
The Mac, so there is no need to determine the user's platform
Prior to calling it.

This version also inverates several minor coding improvements.

** Note that it is possible to set multiple cookies with the same
Name but different (nested) paths. For example:

SetCookie ("color", "red", null, "/outer ");
SetCookie ("color", "blue", null, "/outer/inner ");

However, GetCookie cannot distinguish between these and will return
The first cookie that matches a given name. It is therefore
Recommended that you ** not * use the same name for cookies
Different paths. (Bear in mind that there is * always * a path
Associated with a cookie; if you don't explain icitly specify one,
The path of the setting document is used .)

Revision History:

"Toss Your Cookies" Version (22-Mar-96)
-Added FixCookieDate () function to correct for Mac date bug

"Second Helping" Version (21-Jan-96)
-Added path, domain and secure parameters to SetCookie
-Replaced home-rolled encode/decode functions with Netscape's
New (then) escape and unescape functions

"Free Cookies" Version (December 95)


For information on the significance of cookie parameters, and
And on cookies in general, please refer to the official cookie
Spec,:

Http: www.netscape.com/newsref/std/cookie_spec.html

**************************************** ***************************/

/** // * "Internal" function to return the decoded value of a cookie */
Copy codeThe Code is as follows:
Function getCookieVal (offset ){
Var endstr = document. cookie. indexOf (";", offset );
If (endstr =-1 ){
Endstr = document. cookie. length;
}
Return unescape (document. cookie. substring (offset, endstr ));
}


/** // * Function to correct for 2.x Mac date bug. Call this function
Fix a date object prior to passing it to SetCookie.
IMPORTANT: This function shocould only be called * once *
Any given date object! See example at the end of this document .*/
Copy codeThe Code is as follows:
Function FixCookieDate (date ){
Var base = new Date (0 );
Var skew = base. getTime (); // dawn of (Unix) time-shocould be 0
If (skew> 0) {// snapshot T on the Mac-ahead of its time
Date. setTime (date. getTime ()-skew );
}
}


/** // * Function to return the value of the cookie specified by "name ".
Name-String object containing the cookie name.
Returns-String object containing the cookie value, or null if
The cookie does not exist .*/
Copy codeThe Code is as follows:
Function GetCookie (name ){
Var temp = name + "= ";
Var tempLen = temp. length;
Var cookieLen = document. cookie. length;
Var I = 0;
While (I <cookieLen ){
Var j = I + tempLen;
If (document. cookie. substring (I, j) = temp ){
Return getCookieVal (j );
}
I = document. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return null;
}

/** // * Function to create or update a cookie.
Name-String object containing the cookie name.
Value-String object containing the cookie value. May contain
Any valid string characters.
[ExpiresDate]-Date object containing the expiration data of the cookie. If
Omitted or null, expires the cookie at the end of the current session.
[Path]-String object indicating the path for which the cookie is valid.
If omitted or null, uses the path of the calling document.
[Domain]-String object indicating the domain for which the cookie is
Valid. If omitted or null, uses the domain of the calling document.
[Secure]-Boolean (true/false) value indicating whether cookie transmission
Requires a secure channel (HTTPS ).

The first two parameters are required. The others, if supplied, must
Be passed in the order listed above. To omit an unused optional field,
Use null as a place holder. For example, to call SetCookie using name,
Value and path, you wocould code:

SetCookie ("myCookieName", "myCookieValue", null ,"/");

Note that trailing omitted parameters do not require a placeholder.

To set a secure cookie for path "/myPath", that expires after
Current session, you might code:

SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true );*/
Copy codeThe Code is as follows:
Function SetCookie (name, value, expiresDate, path, domain, secure ){
Document. cookie = name + "=" + escape (value) +
(ExpiresDate )? "; Expires =" + expiresDate. toGMTString (): "") +
(Path )? "; Path =" + path: "") +
(Domain )? "; Domain =" + domain: "") +
(Secure )? "; Secure ":"");
}


/** // * Function to delete a cookie. (Sets expiration date to start of epoch)
Name-String object containing the cookie name
Path-String object containing the path of the cookie to delete. This MUST
Be the same as the path used to create the cookie, or null/omitted if
No path was specified when creating the cookie.
Domain-String object containing the domain of the cookie to delete. This MUST
Be the same as the domain used to create the cookie, or null/omitted if
No domain was specified when creating the cookie .*/
Copy codeThe Code is as follows:
Function DeleteCookie (name, path, domain ){
If (GetCookie (name )){
Document. cookie = name + "=" +
(Path )? "; Path =" + path: "") +
(Domain )? "; Domain =" + domain: "") +
"; Expires = Thu, 01-Jan-70 00:00:01 GMT ";
}
}



// Calling examples:
// Var expdate = new Date ();
// FixCookieDate (expdate); // Correct for Mac date bug-call only once for given Date object!
// Expdate. setTime (expdate. getTime () + (24x60*60*1000); // 24 hrs from now
// SetCookie ("ccpath", "http://www.dupcit.com/articles/", expdate );
// SetCookie ("ccname", "WebWoman", expdate );
// SetCookie ("tempvar", "This is a temporary cookie .");
// SetCookie ("ubiquitous", "This cookie will work anywhere in this domain", null ,"/");
// SetCookie ("paranoid", "This cookie requires secure communications", expdate, "/", null, true );
// SetCookie ("goner", "This cookie must die! ");
// Document. write (document. cookie + "<br> ");
// DeleteCookie ("goner ");
// Document. write (document. cookie + "<br> ");
// Document. write ("ccpath =" + GetCookie ("ccpath") + "<br> ");
// Document. write ("ccname =" + GetCookie ("ccname") + "<br> ");
// Document. write ("tempvar =" + GetCookie ("tempvar") + "<br> ");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.