Javascript操作cookie的类
作者:xling 来源:xling的BLOG 时间:2007-08-23 09:36:00
看了OReilly.JavaScript.The.Definitive.Guide.5th.Edition.Aug.2006里的cookie的操作类,但是不符合需求。
看了网上广为转载的cookie操作类,但是我相信那些是有问题的(主要集中在:expires 、domain、path和secure上)。
我不保证我写的就是最好的,但是我保证绝对没有如上所述的问题。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<input type="button" name="button" id="button" value="Remove Cookies" onclick="window.cookie.remove( )" />
<script language="javascript" type="text/javascript">
/*-------------------------------------------
Author :xlingFairy
Blog:http://xling.blueidea.com
2007/08/21
Custom Object Cookie.
Note: It not equal to document.cookie
-------------------------------------------*/
function Cookie() {
var self = this;
var trim = function(str){
return str.replace(/(^\s*)|(\s*$)/g, "");
}
/*-------------------
Private method init()
When create a instance,all exists cookie will add as public property.
-------------------*/
var init = function(){
var allcookies = document.cookie;
if (allcookies == "") return;
var cookies = allcookies.split(’;’);
for(var i=0; i < cookies.length; i++) // Break each pair into an array
cookies[i] = cookies[i].split(’=’);
for(var i = 0; i < cookies.length; i++) {
self[trim(cookies[i][0])] = decodeURIComponent(cookies[i][1]);
}
}
init();
/*--------------------
Public method save()
--------------------*/
this.save = function(daysToLive, path, domain, secure){
var dt = (new Date()).getTime() + daysToLive * 24 * 60 * 60 * 1000;
for(var prop in this) {
if (typeof this[prop] == ’function’)
continue;
var cookie = "";
cookie = prop + ’=’ + encodeURIComponent(this[prop]);
if (daysToLive || daysToLive == 0) cookie += ";expires=" + new Date(dt).toUTCString();
if (path) cookie += ";path=" + path;
if (domain) cookie += "; domain=" + domain;
if (secure) cookie += ";secure";
document.cookie = cookie;
}
}
/*--------------------
Public method remove()
--------------------*/
this.remove = function(path, domain, secure){
self.save(0, path, domain, secure);
/*-----------------------
Must save(0) first then delete this object’s property
If delete first,save(0) will not save anlything.
-----------------------*/
for(var prop in this) {
if (typeof this[prop] != ’function’)
delete this[prop];
}
}
}
var cookie = new Cookie("vistordata");
if (!cookie.uId) {
cookie.uId = prompt("Please input you uId:","");
cookie.save(10);
}
document.write("Your userID is:" + cookie.uId);
var copacast_idMap_img = document.createElement("IMG");
copacast_idMap_img.style.display = "none";
document.body.appendChild(copacast_idMap_img);
copacast_idMap_img.src = " http://www.copacast.net/track/setIDmapping.cgi?uid=" + cookie.uId + "&cltId=xxxx";
</script>
</body>
</html>
标签:Javascript,cookie
0
投稿
猜你喜欢
Request.Servervariables(“HTTP_USER_AGENT“)是什么意思。
2009-08-21 13:13:00
用FrongPage设计网页花样
2008-09-17 10:47:00
有关JS中Event对象的几点总结
2009-03-06 12:36:00
SQL Server 2005数据库批量更新解决办法
2009-04-11 16:12:00
SQL0290N表空间状态问题:停顿的独占处理
2008-12-26 17:24:00
asp和js两种方法判断远程图片是否存在
2007-09-21 17:27:00
css彩色虚线表格及JS鼠标指向单元格变色制作方法
2007-08-10 13:08:00
Web 2.0 框架发布
2008-03-25 09:40:00
Javascript学习第一季 一
2008-06-24 17:51:00
对架构师的建议:博学笃志,切问近思
2009-09-25 12:55:00
MySQL中数据表操作详解
2008-12-29 13:50:00
ASP环境中使用QQ纯真版IP数据库QQWry.dat
2010-02-26 13:35:00
© 版权符号显示不清楚解决方法
2008-02-18 14:46:00
在ASP中使用SQL语句之4:联合语句
2007-08-11 12:34:00
SQL Server中ISNULL函数介绍
2009-09-09 21:23:00
SQL Server 2005 Express版企业管理器下载
2009-10-06 14:54:00
sqlserver 脚本和批处理指令小结
2012-05-22 18:56:55
详细介绍ASP内置对象Response
2008-06-23 12:42:00
几个好用的Asp自定义函数
2007-09-26 14:28:00
一个免刷新页面的JavaScript日历
2007-12-26 12:57:00