写入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 相关文章推荐
extjs 学习笔记 四 带分页的grid
Oct 20 Javascript
Javascript document.referrer判断访客来源网址
May 15 Javascript
javascript 浏览器检测代码精简版
Mar 04 Javascript
Chrome中模态对话框showModalDialog返回值问题的解决方法
May 25 Javascript
JS+ACTIVEX实现网页选择本地目录路径对话框
Mar 18 Javascript
JavaScript中双叹号!!作用示例介绍
Sep 21 Javascript
使用JavaScript 编写简单计算器
Nov 24 Javascript
jquery实现弹出层效果实例
May 19 Javascript
JS实现日期时间动态显示的方法
Dec 07 Javascript
jQuery实现自动调用和触发某个事件的方法
Nov 18 Javascript
javascript移动端 电子书 翻页效果实现代码
Sep 07 Javascript
小程序接口的promise化的实现方法
Dec 11 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
PHP的FTP学习(二)
2006/10/09 PHP
PHP写UltraEdit插件脚本实现方法
2011/12/26 PHP
php 下载保存文件保存到本地的两种实现方法
2013/08/12 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十)
2014/06/24 PHP
解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法
2015/12/18 PHP
php的4种常用运行方式详解
2016/12/22 PHP
php使用环形链表解决约瑟夫问题完整示例
2018/08/07 PHP
PHP defined()函数的使用图文详解
2019/07/20 PHP
laravel 根据不同组织加载不同视图的实现
2019/10/14 PHP
仿迅雷焦点广告效果(JQuery版)
2008/11/19 Javascript
js表格分页实现代码
2009/09/18 Javascript
Javascript面向对象之四 继承
2011/02/08 Javascript
网页加载时页面显示进度条加载完成之后显示网页内容
2012/12/23 Javascript
jquery限定文本框只能输入数字即整数和小数
2013/11/29 Javascript
JavaScript中number转换成string介绍
2014/12/31 Javascript
Nodejs学习笔记之测试驱动
2015/04/16 NodeJs
浅谈利用JavaScript进行的DDoS攻击原理与防御
2015/06/04 Javascript
详解参数传递四种形式
2015/07/21 Javascript
jQuery+PHP实现可编辑表格字段内容并实时保存
2015/10/09 Javascript
Javascript中字符串replace方法的第二个参数探究
2016/12/05 Javascript
jQuery插件echarts去掉垂直网格线用法示例
2017/03/03 Javascript
AngularJs导出数据到Excel的示例代码
2017/08/11 Javascript
Python 实现一个颜色色值转换的小工具
2016/12/06 Python
使用Python将字符串转换为格式化的日期时间字符串
2019/09/01 Python
python matplotlib 画dataframe的时间序列图实例
2019/11/20 Python
Python GUI之tkinter窗口视窗教程大集合(推荐)
2020/10/20 Python
python time()的实例用法
2020/11/03 Python
Cpython解释器中的GIL全局解释器锁
2020/11/09 Python
Schutz鞋官方网站:Schutz Shoes
2017/12/13 全球购物
Araks官网:纽约内衣品牌
2020/10/15 全球购物
电子商务专业个人的自我评价分享
2013/10/29 职场文书
机电专业个人求职信范文
2013/12/30 职场文书
小学优秀班干部事迹材料
2014/05/25 职场文书
钓鱼岛事件感想
2015/08/11 职场文书
教师听课学习心得体会
2016/01/15 职场文书
Java 多线程并发FutureTask
2022/06/28 Java/Android