写入cookie的JavaScript代码库 cookieLibrary.js


Posted in Javascript onOctober 24, 2009

/* 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
later versions), resulting in "zombie" cookies 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 will not occur.

The FixCookieDate function must now be called explicitly to
correct for the 2.x Mac date bug. This function should be
called *once* after a Date object is created and before it
is passed (as an expiration date) to SetCookie. Because the
Mac date bug affects all dates, not just those passed to
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 than
the Mac, so there is no need to determine the user's platform
prior to calling it.

This version also incorporates 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 with
different paths. (Bear in mind that 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 in 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 */

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 be called *once* for
any 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 0 
if (skew > 0) { // except 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. */

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 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 the
current 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 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. */

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 + "<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>");

Javascript 相关文章推荐
Javascript下判断是否为闰年的Datetime包
Oct 26 Javascript
Jquery 点击按钮显示和隐藏层的代码
Jul 25 Javascript
js 判断控件获得焦点的示例代码
Mar 04 Javascript
js实现图片点击左右轮播
Jul 08 Javascript
分享JavaScript与Java中MD5使用两个例子
Dec 23 Javascript
jquery+json实现动态商品内容展示的方法
Jan 14 Javascript
JS获取年月日时分秒的方法分析
Nov 28 Javascript
AngularJS Controller作用域
Jan 09 Javascript
javascript中apply/call和bind的使用
Feb 15 Javascript
JS对日期操作封装代码实例
Nov 08 Javascript
JavaScript实现简易计算器小功能
Oct 22 Javascript
JavaScript如何实现防止重复的网络请求的示例
Jan 28 Javascript
js文件中调用js的实现方法小结
Oct 23 #Javascript
struts2 jquery 打造无限层次的树
Oct 23 #Javascript
jquery 插件开发方法小结
Oct 23 #Javascript
jquery 屏蔽一个区域内的所有元素,禁止输入
Oct 22 #Javascript
Domino中运用jQuery读取视图内容的方法
Oct 21 #Javascript
JavaScript 常用函数库详解
Oct 21 #Javascript
再谈ie和firefox下的document.all属性
Oct 21 #Javascript
You might like
第十节 抽象方法和抽象类 [10]
2006/10/09 PHP
PHP实现的简单三角形、矩形周长面积计算器分享
2014/11/18 PHP
JQuery 选择和过滤方法代码总结
2010/11/19 Javascript
IE6 fixed的完美解决方案
2011/03/31 Javascript
带左右箭头图片轮播的JS代码
2013/12/18 Javascript
原生js实现移动开发轮播图、相册滑动特效
2015/04/17 Javascript
整理JavaScript对DOM中各种类型的元素的常用操作
2016/05/05 Javascript
JavaScript调试的多个必备小Tips
2017/01/15 Javascript
javascript设计模式之策略模式学习笔记
2017/02/15 Javascript
纯JS实现简单的日历
2017/06/26 Javascript
JS基于递归实现网页版计算器的方法分析
2017/12/20 Javascript
原生JS实现微信通讯录
2020/06/18 Javascript
使用node-media-server搭建一个简易的流媒体服务器
2021/01/20 Javascript
Python中实现三目运算的方法
2015/06/21 Python
django2 快速安装指南分享
2018/01/05 Python
TensorFlow实现AutoEncoder自编码器
2018/03/09 Python
Caffe均值文件mean.binaryproto转mean.npy的方法
2018/07/09 Python
python按时间排序目录下的文件实现方法
2018/10/17 Python
python通过robert、sobel、Laplace算子实现图像边缘提取详解
2019/08/21 Python
Python完全识别验证码自动登录实例详解
2019/11/24 Python
Python3的socket使用方法详解
2020/02/18 Python
美国受欢迎的眼影品牌:BH Cosmetics
2016/10/25 全球购物
Shopee马来西亚:随拍即卖,最佳行动电商拍卖平台
2017/06/05 全球购物
车库门开启器、遥控器和零件:Chamberlain
2019/04/09 全球购物
Java程序员面试题
2013/07/15 面试题
财务会计专业求职信范文
2013/12/31 职场文书
高中生职业生涯规划书
2014/02/24 职场文书
小学向国旗敬礼活动方案
2014/09/27 职场文书
11.9消防日宣传标语
2014/10/08 职场文书
留学推荐信怎么写
2015/03/26 职场文书
2015年基层党建工作总结
2015/05/14 职场文书
大国崛起日本观后感
2015/06/02 职场文书
2016中秋节晚会开场白
2015/11/26 职场文书
MongoDB balancer的使用详解
2021/04/30 MongoDB
详解MySQL中timestamp和datetime时区问题导致做DTS遇到的坑
2021/12/06 MySQL
MIME类型中application/xml与text/xml的区别介绍
2022/01/18 HTML / CSS