JavaScript实现cookies设置【读,增,删,修等操作】

无名氏 WEB前端 1,234 次浏览 , , JavaScript实现cookies设置【读,增,删,修等操作】已关闭评论

/*GetCookie(name) 获取Cookies方法 ,name为名字
SetCookie(name,value,interval,number,[path],[domain],[secure]) 设置cookies方法
name 名称,value cookies值,interval(”y” 年,”q”季,”m”月,”d”日,”h”时,”M”分,”s”秒,默认:日),number:数量级
[path],[domain],[secure] 可缺省。
DelCookie(name) 删除设置过期cookies , name:cookies名称
mr.junval@qq.com 2008
*/
function GetCookieVal(offset){
var endstr = document.cookie.indexOf (“;”, offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name){
var arg = name + “=”;
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen){
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(” “, i) + 1;
if (i == 0) break;
}
return null;
}

function DelCookie(name){
var exp = new Date();
exp.setTime (exp.getTime() – 1);
var cval = GetCookie (name);
document.cookie = name + “=” + cval + “; expires=”+ exp.toGMTString();
}

function SetCookie(name, value,interval,number){
if (interval == “0”) return false;
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = DateAdd(interval,number,expdate);
var path = (argc > 4) ? argv[4] : null;
var domain = (argc > 5) ? argv[5] : null;
var secure = (argc > 6) ? argv[6] : false;
document.cookie = name + “=” + escape (value) +((expires == null) ? “” : (“; expires=”+ expires.toGMTString()))
+((path == null) ? “” : (“; path=” + path)) +((domain == null) ? “” : (“; domain=” + domain))
+((secure == true) ? “; secure” : “”);
}

function DateAdd(interval,number,date){
switch(interval){
case “y”:{
date.setFullYear(date.getFullYear()+number);
return date;
break;
}
case “q”:{
date.setMonth(date.getMonth()+number*3);
return date;
break;
}
case “m”:{
date.setMonth(date.getMonth()+number);
return date;
break;
}
case “w”:{
date.setDate(date.getDate()+number*7);
return date;
break;
}
case “d”:{
date.setDate(date.getDate()+number);
return date;
break;
}
case “h”:{
date.setHours(date.getHours()+number);
return date;
break;
}
case “M”:{
date.setMinutes(date.getMinutes()+number);
return date;
break;
}
case “s”:{
date.setSeconds(date.getSeconds()+number);
return date;
break;
}
case “0”:{
return date;
break;
}
}
}

Go