87,917
社区成员
发帖
与我相关
我的任务
分享
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
{
//取值
}
}