js对cookie的操作
2016-04-27 15:48
585 查看
var mycookies = { set:function(name, value){ var cook = name+"="+value+";"; if(!!arguments[2]){ var d = new Date(); d.setDate(d.getDate()+arguments[2]); cook = cook + "expires="+d.toUTCString()+";"; } if(!!arguments[3]){ cook = cook + "path="+arguments[3]+";"; } if(!!arguments[4]){ cook = cook + "domain="+arguments[4]; } document.cookie = cook; }, get:function(name){ var cookie = document.cookie; var arr = cookie.split(";"); for(var i=0;i<arr.length;i++){ var info = arr[i]; var arrInfo = info.split("="); if(arrInfo[0]==name){ return arrInfo[1]; } } return ""; }, remove:function(name){ var info = this.get(name); var date = new Date(); date.setDate(date.getDate()-1); var cook = name + "=" + info+";expires="+date.toUTCString()+";"; if(!!arguments[1]){ cook = cook + "path="+arguments[1]+";"; } if(!!arguments[2]){ cook = cook + "domain="+arguments[2]+";"; } document.cookie = cook; } }
相关文章推荐