Web前端JS操作cookie

前端开发中cookie it技术博客|前端开发中怎么处理缓存|前端开发清理缓存插件
设置cookie:
function setCookie(name,value,days,path){
var t = new Date(),
sCookie= name + “=” + escape(value);
t.setTime(t.getTime()+days2436001000);
sCookie += “;expires=”+t.toGMTString();
if(path){
sCookie += “;path=”+path;
}
document.cookie = sCookie;
}
查询cookie:
function getCookie(name){
var preg = new RegExp(“(^| )”+name+”=([^;])(;|$)”,”g”);
if(preg.test(document.cookie)){
return RegExp.$2;
}else{
return “”;
}
}
删除cookie:
function delCookie(name){
setCookie(name,””,-1);
}
清空cookie:
function clearCookie(){
var cookieArr = document.cookie.split(“;”);
for(var i=0;i<cookieArr.length;i++){
var preg = /([^;])=([^;])/;
var match = preg.exec(document.cookie);
delCookie(match[1]);
}
}
简单的优化封装:
var cookie = {
set : function(name,value,days,path){
var t = new Date(),
sCookie= name + “=” + escape(value);
t.setTime(t.getTime()+days2436001000);
sCookie += “;expires=”+t.toGMTString();
sCookie += path && “;path=”+path || “”;
document.cookie = sCookie;
},
get : function(name){
var preg = new RegExp(“(^| )”+name+”=([^;])(;|$)”,”g”);
return preg.test(document.cookie) && RegExp.$2 || “”;
},
del : function(name){
cookie.set(name,””,-1);
},
cls : function(){
var cookieArr = document.cookie.split(“;”);
for(var i=0;i<cookieArr.length;i++){
cookie.del(/([^;])=([^;])/.exec(document.cookie)[1]);
}
}
}
现在前端开发主要技术|前端开发手机调试|web前端开发怎么插入表格
» 本文来自:前端开发者 » 《Web前端JS操作cookie》
» 本文链接地址:https://www.rokub.com/5242.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!