javascript操作クッキーノート
1352 ワード
function Cookie(){
// cookie
this.put = function (name,value){
if((""+value).indexOf(";")!=-1){
document.cookie = name+"="+escape(value.substring(0,value.indexOf(";")))+value.substring(value.indexOf(";"));
}else{
document.cookie = name+"="+escape(value);
}
};
// cookie
this.get = function(name){
return unescape(this.getAll()[name]);
};
// cookie
this.getAll = function(){
var _cache = {};
var _cs = document.cookie;
if(_cs){
var _a = _cs.split("; ");// TODO
for(var i=0; i<_a.length; i++){
_cache[_a[i].substring(0,_a[i].indexOf("="))]=unescape(_a[i].substring(_a[i].indexOf("=")+1));
}
}
return _cache;
};
// cookie
this.remove = function(name){
this.setTimeout(name,-1);
};
// ,
this.setTimeout = function(name,ms){
var reg = /(-)?\d*/;
if(reg.test(ms)){
var exp = new Date();
exp.setTime(exp.getTime() + ms);
this.put(name,this.get(name)+";expires="+exp.toGMTString());
}
}
}