cookie封装
<script>
window.onload=function (ev) {
// var date=new Date();
// date.setDate(date.getDate()+1);
// document.cookie="age=33; expires="+date.toGMTString()+";";
// document.cookie="age=33;name=ln;";
// alert(document.cookie);
// addCookie("wwe","99");
// addCookie("gender","99",7,"/");
function addCookie(key,value,day,path,domain){
// 1.处理默认保存的路径
// if(!path){
// var index=window.location.pathname.lastIndexOf("/");
// var currentPath=window.location.pathname.slice(0,index);
// path=currentPath;
// }
var index=window.location.pathname.lastIndexOf("/");
var currentPath=window.location.pathname.slice(0,index);
path=path||currentPath;
// 2.处理默认保存的时间
domain=domain||document.domain;
// 3.处理默认过期的时间
if(!day){//没有输入day值的话
document.cookie=key+"="+value+";path="+path+";domain="+domain+";";
} else{
var date=new Date();
date.setDate(date.getDate()+day);
document.cookie=key+"="+value+";expires="+date.toGMTString()+";path="+path+";domain="+domain+";";
}
}
function getCookie(key) {
// console.log(document.cookie);
var res=document.cookie.split(";");
// console.log(res);
for(var i=0;i<res.length;i++){
// console.log(res[i]);
var temp=res[i].split("=");
// console.log(temp);
// if(temp[0].trim()==="scroe"){
// console.log(temp[1]);
// }
if(temp[0].trim()===key){
return temp[1];
}
}
}//封装
console.log(getCookie("age"));
// function delCookie(key) {
// addCookie(key,getCookie(key),-1);
// }
// delCookie("scroe");//可删除默认路径的值
function delCookie(key, path) {
addCookie(key,getCookie(key),-1,path);
}
delCookie("gender","/");//可删除修改后的路径(在删除的时候指定路径)
}
</script>
可以更改key的值来实现封装;