写入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 相关文章推荐
JQuery 初体验(建议学习jquery)
Apr 25 Javascript
Jquery中对数组的操作代码
Aug 12 Javascript
Js,alert出现乱码问题的解决方法
Jun 19 Javascript
js几秒以后倒计时跳转示例
Dec 26 Javascript
jquery实现页面虚拟键盘特效
Aug 08 Javascript
10个JavaScript中易犯小错误
Feb 14 Javascript
使用RequireJS库加载JavaScript模块的实例教程
Jun 06 Javascript
jQuery实现鼠标经过显示动画边框特效
Mar 24 jQuery
arcgis for js栅格图层叠加(Raster Layer)问题
Nov 22 Javascript
使用vue实现简单键盘的示例(支持移动端和pc端)
Dec 25 Javascript
jQuery 导航自动跟随滚动的实现代码
May 30 jQuery
vue2.0页面前进刷新回退不刷新的实现方法
Jul 31 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
PHP4(windows版本)中的COM函数
2006/10/09 PHP
浅谈php中mysql与mysqli的区别分析
2013/06/10 PHP
php中AES加密解密的例子小结
2014/02/18 PHP
PHP的preg_match匹配字符串长度问题解决方法
2014/05/03 PHP
如何简单地用YUI做JavaScript动画
2007/03/10 Javascript
仿百度的关键词匹配搜索示例
2013/09/25 Javascript
js如何获取兄弟、父类等节点
2014/01/06 Javascript
js实现回放拖拽轨迹从过程上进行分析
2014/06/26 Javascript
javascript 获取函数形参个数
2014/07/31 Javascript
jquery实现漫天雪花飞舞的圣诞祝福雪花效果代码分享
2015/08/20 Javascript
Jquery实现简单的轮播效果(代码管用)
2016/03/14 Javascript
js基于setTimeout与setInterval实现多线程
2016/06/17 Javascript
JavaScript toUpperCase()方法使用详解
2016/08/26 Javascript
jquery加载单文件vue组件的方法
2017/06/20 jQuery
vue注册组件的几种方式总结
2018/03/08 Javascript
vue中实现先请求数据再渲染dom分享
2018/03/17 Javascript
详解Angular操作cookies方法
2018/06/01 Javascript
Vue cli3 库模式搭建组件库并发布到 npm的流程
2018/10/12 Javascript
vue使用vant中的checkbox实现全选功能
2020/11/17 Vue.js
Python基于OpenCV实现视频的人脸检测
2018/01/23 Python
详解Python用户登录接口的方法
2019/04/17 Python
Python闭包和装饰器用法实例详解
2019/05/22 Python
美国女士时尚珠宝及配饰购物网站:Icing
2018/07/02 全球购物
解释DataSet(ds) 和 ds as DataSet 的含义
2014/07/27 面试题
社区志愿者心得体会
2014/01/03 职场文书
市场营销职业生涯规划书范文
2014/01/12 职场文书
初三物理教学反思
2014/01/21 职场文书
企业总经理岗位职责
2014/02/13 职场文书
中华魂放飞梦想演讲稿
2014/08/26 职场文书
公安机关查摆剖析材料
2014/10/10 职场文书
2014年设计师工作总结
2014/11/25 职场文书
2014年组织委员工作总结
2014/12/01 职场文书
2014年创先争优工作总结
2014/12/11 职场文书
工作能力自我评价2015
2015/03/05 职场文书
《世界多美呀》教学反思
2016/02/22 职场文书
创业分两种人:那么哪些适合创业?,哪些适合不适合创业呢?
2019/08/23 职场文书