function JSCookie()
{
this.GetCookie = function(key)
{
var value = document.cookie.match(new RegExp(";?"+key+"=([^;]*)","i"));
return unescape(value?value[1]:"");
};
this.GetChild = function(cookiekey,childkey)
{
var child = this.GetCookie(cookiekey);
var getvalue = child.match(new RegExp("&?"+childkey+"=([^&;]*)","i"));
return unescape(getvalue?getvalue[1]:"");
};
this.SetCookie = function(key,value,expire,path)
{
var cookie = "";
if(key && value) cookie += key + "=" + escape(value) + ";";
if(expire) cookie += "expires=" + expire.toGMTString() + ";";
if(path) cookie += "path=" + path + ";";
document.cookie = cookie;
};
this.Expire = function(key)
{
expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() - 1);
var cookie = " " + key + "=e;expires=" + expire_time + ";"
document.cookie = cookie;
}
}
用法:
一、设置cookie
var cookie = new JSCookie();
//普通设置
cookie.SetCookie("key1","val1");
//过期时间为一年
var expire_time = new Date();
expire_time.setFullYear(expire_time.getFullYear() + 1);
cookie.SetCookie("key2","val2",expire_time);
//设置路径,带过期时间
cookie.SetCookie("key3","val3",expire_time,"/");
//设置带子键的cookie,子键分别是k1,k2,k3
cookie.SetCookie("key4","k1=1&k2=2&k3=3");
二、读取cookie
//简单获取
cookie.GetCookie("key1");
cookie.GetCookie("key2");
cookie.GetCookie("key3");
cookie.GetCookie("key4");
//获取key4的子键k1值
cookie.GetChild("key4","k1");
三、删除
cookie.Expire("key1");
cookie.Expire("key2");
cookie.Expire("key3");
cookie.Expire("key4");
590

被折叠的 条评论
为什么被折叠?



