您的位置:首页 > Web前端 > JavaScript

JS操作Cookie

2015-11-23 18:07 441 查看
操作Cookie是为了短暂的保存一些东西,你可以选择用JS操作,也可以选择后台的Cookie操作,后台操作Cookie在JS里面也是可以获取到cookie

//添加cookie

functionaddCookie(productid,photo,content,price,name){
varproductids=jQuery.trim(productid);
  
  //这是你需要保存的cookie,用加号拼接,是为了后面的截取,这个可以随意变换
varstr=productids+"+"+photo+"+"+content+"+"+price+"="+escape(name);
//getCookie(productids);
varobjHours=0;
if(objHours>0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
vardate=newDate();
varms=objHours*60*1000;
date.setTime(date.getTime()+ms);
str+=";expires="+date.toGMTString();
}
document.cookie=str;
}


//查询所有的cookie
functionallCookie(){

varstr=document.cookie;//获取所有的cookie
vararrStr=document.cookie.split(";");//分割cookie后的字符串

}

//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间

functiondelCookie(name){
  vardate=newDate();
  date.setTime(date.getTime()-10000);
  document.cookie=name+"=a;expires="+date.toGMTString();
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: