function Cookie(name,value,options)
{
if(typeof value!="undefined")
{
options=options||{};
if(value===null)
{
value='';
options.expires=-1;//设置失效时间
}
var expires='';
//判断类型是number,分别设置时间
if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString))
{
var date;
if(typeof options.expires=='number')
{
//设置时间格式,把天转换为毫秒添加倒时间对象
date=new Date();
date.setTime(date.getTime()+(options.expires*24*60*60*1000));
}
else
{
date=options.expires;
}
expires = '; expires=' + date.toUTCString();
}
var path=options.path?';path='+options.path:'';  //路径
var domain=options.domain?';domain='+options.domain:'';  //设置域
var secure=options.secure?';secure':''; //安全措施,true直接设置
//把所有字符串存入数组,然后调用Join()方法转换为字符串  并写入cookie
document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');
}
else
{
//取值
}
}上面是我生存cookie的一段代码,为什么我设置了path以后就不稳定了啊。
就是第一次可以赋值、取值都行,第二次就不能再设置cookie了,获取到的值一直是空。
不设置path就很好使。添加、删除、获取都可以。请高手看看代码有问题吗
//添加cookie
Cookie('user',"java" ,{path:'/'})
//获取cookie
Cookie('user')
//删除cookie
Cookie('user',null)