跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

写入cookie的JavaScript代码库 cookieLibrary.js_javascript技巧

/* Cookie Library -- "Night of the Living Cookie" Version (25-Jul-96) 2缔友计算机信息技术有限公司,涂聚文 geovindu@163.com 互相交流3 Written by: Bill Dortch, hIdaho Design 4 The following functions are released to the public domain.5http://www.dusystem.com/6 This version takes a more aggressive approach to deleting7cookies. Previous versions set the expiration date to one8 millisecond prior to the current time; however, this method9 did not work in Netscape 2.02 (though it does in earlier andlater versions), resulting in "zombie" cookies that would notdie. DeleteCookie now sets the expiration date to the earliestusable date (one second into 1970), and sets the cookie's valueto null for good measure.Also, this version adds optional path and domain parameters tothe DeleteCookie function. If you specify a path and/or domainwhen creating (setting) a cookie**, you must specify the samepath/domain when deleting it, or deletion will not occur.The FixCookieDate function must now be called explicitly tocorrect for the 2.x Mac date bug. This function should becalled *once* after a Date object is created and before itis passed (as an expiration date) to SetCookie. Because theMac date bug affects all dates, not just those passed toSetCookie, you might want to make it a habit to callFixCookieDate any time you create a new Date object:var theDate = new Date();FixCookieDate (theDate);Calling FixCookieDate has no effect on platforms other thanthe Mac, so there is no need to determine the user's platformprior to calling it.This version also incorporates several minor coding improvements.**Note that it is possible to set multiple cookies with the samename 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 returnthe first cookie that matches a given name. It is thereforerecommended that you *not* use the same name for cookies withdifferent paths. (Bear in mind that there is *always* a pathassociated 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'snew (then) escape and unescape functions"Free Cookies" Version (December 95)For information on the significance of cookie parameters, andand on cookies in general, please refer to the official cookiespec, at:http:www.netscape.com/newsref/std/cookie_spec.html****************************************************************** *//**//* "Internal" function to return the decoded value of a cookie */复制代码代码如下: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 tofix a date object prior to passing it to SetCookie.IMPORTANT: This function should only be called *once* forany given date object! See example at the end of this document. */复制代码代码如下:function FixCookieDate (date) {var base = new Date(0);var skew = base.getTime(); // dawn of (Unix) time - should be 0if (skew > 0) { // except on the Mac - ahead of its timedate.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 ifthe cookie does not exist. */复制代码代码如下:function GetCookie (name) {var temp = name + "=";var tempLen = temp.length;var cookieLen = document.cookie.length;var i = 0;while (i 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 containany valid string characters.[expiresDate] - Date object containing the expiration data of the cookie. Ifomitted 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 isvalid. If omitted or null, uses the domain of the calling document.[secure] - Boolean (true/false) value indicating whether cookie transmissionrequires a secure channel (HTTPS).The first two parameters are required. The others, if supplied, mustbe 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 would 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 thecurrent session, you might code:SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true); */复制代码代码如下: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 namepath - String object containing the path of the cookie to delete. This MUSTbe the same as the path used to create the cookie, or null/omitted ifno path was specified when creating the cookie.domain - String object containing the domain of the cookie to delete. This MUSTbe the same as the domain used to create the cookie, or null/omitted ifno domain was specified when creating the cookie. */复制代码代码如下: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)); // 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 + "");// DeleteCookie ("goner");// document.write (document.cookie + "");// document.write ("ccpath = " + GetCookie("ccpath") + "");// document.write ("ccname = " + GetCookie("ccname") + "");// document.write ("tempvar = " + GetCookie("tempvar") + "");

相关文章