cookie的封装删除修改

一、设置
        
name = value;
path = 路径;
expires = new Date();
var d = new Date();
d.setDate(30)
document.cookie = "password = tp;path = /;expires = "+d
 
var d2 = new Date();
d2.setDate(30)
document.cookie = "password = 123456;path = /; expires = " +d
 
        封装
            name value 一定会改;
            path expires 不一定会改
    
    分离 
    
function setCookie (name , value,path, expires){
    //根据参数判断是否拼接path 和expires;
    var str = name + "="+value;
    if(path){
        str += "; path=" + path;
    }
    if(expires){
        var d = new Date();
        d.setDate(d.getDate() + expires)
        str += ";expires = " +d;
    }
    console.log(str);
    document.cookie = str;
}
 
字符串拼接的封装
    
setCookie ("sss","hhhh","/cookie",20)
 
    如果不定参(非必选参数)有多个,我们一般会把这样的参数放进一个对象之中
    
function setCookie (name,value,options){
    //根据参数判定是否拼接path和expires;
    var str = name + "=" + value;
    if(options.path){
        str += ";path =" + options.path;
    }
    if(option.expires) {
        var d = new Date();
        d.setDate(d.getDate() + options.expires) 
        str += ";expires = " +d;
    }
        console.log(str);
        document.cookie = str;
}
    
setCookie ("hhh","dddd",{
    expires:30
})
 
优雅封装
function setCookie (name,value,options){
    document.cookie = (function(name,value,options){
    var str = name + "="+value;
    if(options.path){
        str += ";path ="+"options.path
    } 
    if(options.expires){
        var d = new Date();
        d.setDate(d.getDate()+options.expires)
        str += ";expires =" +d;
     }
        return str;
}) (name,value,options)       
}
 
setCookie ("hhh","dddd",{
    expires:30
})
 
删除
    
function removeCookie (name,path){
    setCookie(name,"",{
        expires : -1,
        path : path
    })
}
removeCookie ("hhhh")
 
获取
    
function setCookie (name,value,options){
    docunment.cookie = (function(name,value,options){
        var str = name + "=" + value;
            if(options.path){
            str += ";path = " + options.path;
        }
            if(options.expires){
                var d = new Date();
                d.setDate(d.getDate() + options.expires)
                str += ";expires = " + d;
        }
            return str;
    })(name,value,options)
}
 
setCookie ("username","tp",{
    expions:10
})
 
setCookie ("password" ,"123456",{
    expires :1 0
})
 
    1、每条cookie 以  ; 空格进行分割;
    2、key 和 value 以 = 进行分割;
 
function getCookie (key){
    var str = document.cookie ;
    //字符串转化成数组
   // string.split() ; 切割字符串;
    var arr = str.split(";")
    //console.log(str,arr);
    //var res = arr.map(function(item,index){
       // if(key == item.split("=")[0]){
         //  return (item.split("=")[1]);
        }
    return false
    })
    for(var i = 0; i < arr.length; i ++){
        if(key === arr[i].split("=")[0]){
            return arr[i].split("=")[1];
       }
    }
    console.og(res)
    return "";
}
console.log(getCookie ("username"));
 
 
原文地址:https://www.cnblogs.com/TianPeng2/p/9992954.html