导航

cookies封装

Posted on 2015-08-14 10:31  网名还没想好  阅读(254)  评论(0编辑  收藏  举报
/**
* @author wxf
*/
var cookie=new function(){
this.set=function(name,value,hours){
var life=new Date().getTime();
life+=hours*1000*60*60;
var cookieStr=name+"="+escape(value)+";expires="+new Date(life).toGMTString()+";path=/";
document.cookie=cookieStr;
};
this.get=function(name){
var cookies = document.cookie.split("; ");
var i = 0;
for(i=0; i<cookies.length; i++) {
var cookie2=cookies[i].split("=");
if(cookie2[0]==name) {return unescape(cookie2[1]);}
}
return '';
};
this.remove=function(name){
var cookieStr=name+"="+escape('null')+";expires="+new Date().toGMTString();
document.cookie=cookieStr;
};
}