JavaScript code library to write cookies Cookielibrary.js_javascript tips

Source: Internet
Author: User
Tags setcookie
/* Cookie Library--"Night of the Living Cookie" Version (25-jul-96)
2 Association of Computer Information Technology Co., Ltd., TU 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.
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
Later versions), resulting in "zombie" cookie that would 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 to
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 won't occur.

The Fixcookiedate function must now is called explicitly to
Correct for the 2.x Mac date bug. This function should is
Called *once* after a Date object is created and before it
is passed (as a expiration date) to Setcookie. Because the
Mac date bug affects all dates, not just those passed to
Setcookie, you might want for 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 than
The MAC, so there are no need to determine the user ' s platform
Prior to calling it.

This version also incorporates several minor coding improvements.

**note that it's 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 would return
The matches a given name. It is therefore
Recommended that you *not* use the same name for cookies with
Different paths. (Bear in mind this there is *always* a path
associated with a cookie; If you don ' t explicitly 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 by general, please refer to the official cookie
Spec, at:

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

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

/**//* "Internal" function to return the decoded value of a cookie * *
Copy Code code 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 to
Fix a Date object prior to passing it to Setcookie.
Important:this function should only is called *once* for
Any given date object! Example at the ' This document. */
Copy Code code as follows:

function Fixcookiedate (date) {
var base = new Date (0);
var skew = Base.gettime (); Dawn of (Unix) Time-should 0
if (Skew > 0) {//except on the mac-ahead of it 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 Code code 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]-The Date object containing the expiration data of the cookie. If
omitted or NULL, expires the cookie at the "end of" session.
[Path]-String object indicating the path for which the cookies is valid.
If omitted or NULL, uses the path of the calling document.
[Domain]-String object indicating the domain for which the cookies 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 two parameters are required. The others, if supplied, must
Be passed in the order listed above. To omit a unused optional field,
Use NULL as a place holder. For example, to call Setcookie using name,
Value and path, you would code:

Setcookie ("Mycookiename", "Mycookievalue", NULL, "/");

Note that trailing omitted parameters does not require a placeholder.

To set a secure cookie for path "/mypath", which expires after the
Current sessions, you might code:

Setcookie (Mycookievar, Cookievaluevar, NULL, "/mypath", null, TRUE); */
Copy Code code 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
Being the same as the path used to create the cookie, or null/omitted if
No path is specified when creating the cookie.
Domain-string object containing the domain of the cookie to delete. This must
Being the same as the domain used to create the cookie, or null/omitted if
No domain is specified when creating the cookie. */
Copy Code code 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 () + (24 * 60 * 60 * 1000)); Hrs from
Setcookie ("Ccpath", "http://www.dupcit.com/articles/", expdate);
Setcookie ("CCName", "Webwoman", expdate);
Setcookie ("TempVar", "This is a temporary cookie.");
Setcookie ("ubiquitous", "this cookie would 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.