写入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 chrome浏览器判断代码
Mar 28 Javascript
Jquery读取URL参数小例子
Aug 30 Javascript
JavaScript学习笔记之JS函数
Jan 22 Javascript
JS清除文本框内容离开在恢复及鼠标离开文本框时触发js的方法
Jan 12 Javascript
深入理解jQuery之防止冒泡事件
May 24 Javascript
浅析JS获取url中的参数实例代码
Jun 14 Javascript
jquery实现页面加载效果
Feb 21 Javascript
js利用for in循环获取 一个对象的所有属性以及值的实例
Mar 30 Javascript
mpvue跳转页面及注意事项
Aug 03 Javascript
使用vue打包进行云服务器上传的问题
Mar 02 Javascript
es6函数之箭头函数用法实例详解
Apr 25 Javascript
vue 给数组添加新对象并赋值
Apr 20 Vue.js
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 防恶意刷新实现代码
2010/05/16 PHP
VIM中设置php自动缩进为4个空格的方法详解
2013/06/14 PHP
php+Mysqli利用事务处理转账问题实例
2015/02/11 PHP
PHP 9 大缓存技术总结
2015/09/17 PHP
laravel学习教程之存取器
2016/07/30 PHP
php-msf源码详解
2017/12/25 PHP
jquery mobile事件多次绑定示例代码
2013/09/13 Javascript
js的正则test,match,exec详细解析
2014/01/29 Javascript
jQuery实现tab选项卡效果的方法
2015/07/08 Javascript
浅谈jQuery添加的HTML,JS失效的问题
2016/10/05 Javascript
微信小程序 Tab页切换更新数据
2017/01/05 Javascript
BootStrap便签页的简单应用
2017/01/06 Javascript
完美解决浏览器跨域的几种方法(汇总)
2017/05/08 Javascript
详谈js对url进行编码和解码(三种方式的区别)
2017/08/16 Javascript
vue.js实现标签页切换效果
2018/06/07 Javascript
JavaScript的Proxy可以做哪些有意思的事儿
2019/06/15 Javascript
[56:41]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 Newbee vs OG
2018/04/01 DOTA
python发送邮件示例(支持中文邮件标题)
2014/02/16 Python
python获取图片颜色信息的方法
2015/03/18 Python
利用Python的Twisted框架实现webshell密码扫描器的教程
2015/04/16 Python
完美解决在oj中Python的循环输入问题
2018/06/25 Python
Django实现表单验证
2018/09/08 Python
浅谈OpenCV中的新函数connectedComponentsWithStats用法
2020/07/05 Python
Python3+selenium配置常见报错解决方案
2020/08/28 Python
Python 下载Bing壁纸的示例
2020/09/29 Python
Python通过fnmatch模块实现文件名匹配
2020/09/30 Python
Python爬虫之Selenium实现窗口截图
2020/12/04 Python
德国家具在线:Fashion For Home
2017/03/11 全球购物
override和overload的区别
2016/03/09 面试题
开水果连锁店创业计划书
2013/12/29 职场文书
财务信息服务专业自荐书范文
2014/02/08 职场文书
机关搬迁方案
2014/05/18 职场文书
中文专业自荐书
2014/06/29 职场文书
计算机实训报告总结
2014/11/05 职场文书
网络销售员岗位职责
2015/04/11 职场文书
创建文明城市倡议书
2015/04/28 职场文书