写入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 相关文章推荐
js tab 选项卡
Apr 26 Javascript
javascript parseInt() 函数的进制转换注意细节
Jan 08 Javascript
ajax与302响应代码测试
Oct 23 Javascript
jquery 表格排序、实时搜索表格内容(附图)
May 19 Javascript
javascript实现博客园页面右下角返回顶部按钮
Feb 22 Javascript
js实现简单计算器
Nov 22 Javascript
JavaScript中的跨浏览器事件操作的基本方法整理
May 20 Javascript
手机端图片缩放旋转全屏查看PhotoSwipe.js插件实现
Aug 25 Javascript
详解Vue.use自定义自己的全局组件
Jun 14 Javascript
vue2.0实现分页组件的实例代码
Jun 22 Javascript
JS通过调用微信API实现微信支付功能的方法示例
Jun 29 Javascript
jQuery代码优化方法总结
Jan 29 jQuery
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
正义联盟的终局之战《天启星战争》将成为DC动画宇宙的最后一部
2020/04/09 欧美动漫
PHP图片自动裁切应付不同尺寸的显示
2014/10/16 PHP
PHP变量赋值、代入给JavaScript中的变量
2015/06/29 PHP
javascript去掉前后空格的实例
2013/11/07 Javascript
jquery的live使用注意事项
2014/02/18 Javascript
jquery uploadify 在FF下无效的解决办法
2014/09/26 Javascript
javascript基于DOM实现权限选择实例分析
2015/05/14 Javascript
js+cookies实现悬浮购物车的方法
2015/05/25 Javascript
JS+CSS实现简易实用的滑动门菜单效果
2015/09/18 Javascript
nodejs简单实现操作arduino
2016/09/25 NodeJs
D3.js进阶系列之CSV表格文件的读取详解
2017/06/06 Javascript
javascript 中事件冒泡和事件捕获机制的详解
2017/09/01 Javascript
Openlayers实现距离面积测量
2020/09/28 Javascript
[02:14]完美“圣”典2016风云人物:xiao8专访
2016/12/01 DOTA
详解Python中的文本处理
2015/04/11 Python
python 按照固定长度分割字符串的方法小结
2018/04/30 Python
pycharm下查看python的变量类型和变量内容的方法
2018/06/26 Python
Python数据集切分实例
2018/12/08 Python
python实现的读取网页并分词功能示例
2019/10/29 Python
python tqdm 实现滚动条不上下滚动代码(保持一行内滚动)
2020/02/19 Python
python脚本实现mp4中的音频提取并保存在原目录
2020/02/27 Python
解决H5的a标签的download属性下载service上的文件出现跨域问题
2019/07/16 HTML / CSS
Capitol Lighting的1800lighting.com:住宅和商业照明
2019/04/10 全球购物
KIKO MILANO西班牙官网:意大利领先的化妆品和护肤品品牌
2019/05/03 全球购物
Pandora德国官网:购买潘多拉手链、戒指、项链和耳环
2020/02/20 全球购物
家长会演讲稿
2014/04/26 职场文书
社会稳定风险评估方案
2014/06/02 职场文书
拆迁委托协议书
2014/09/15 职场文书
聚会通知怎么写
2015/04/23 职场文书
护理专业毕业自我鉴定
2019/08/12 职场文书
创业计划书之废品回收
2019/09/26 职场文书
实操Python爬取觅知网素材图片示例
2021/11/27 Python
python接口测试返回数据为字典取值方式
2022/02/12 Python
nginx.conf配置文件结构小结
2022/04/08 Servers
python和anaconda的区别
2022/05/06 Python
一文解答什么是MySQL的回表
2022/08/05 MySQL