cookie和localStorage常用方法

阅读更多

 

cookie读、设、删:

function addcookie(name,value,expireHours){
    var cookieString=name+"="+escape(value)+"; path=/";
    //判断是否设置过期时间
    if(expireHours>0){
        var date=new Date();
        date.setTime(date.getTime+expireHours*3600*1000);
        cookieString=cookieString+"; expire="+date.toGMTString();
    }
    document.cookie=cookieString;
}

function getcookie(name){
    var strcookie=document.cookie;
    var arrcookie=strcookie.split("; ");
    for(var i=0;i 
  

 

 

localStorage读、设、删:

localStorage.getItem( key );
localStorage.setItem( key, value );
localStorage.removeItem( key );

 

 

 

清空cookie

function clearCookie() {
                var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
                if(keys){
                    for(var i=0;i 
  

 

 

 

清空localStorage

function clearLocal() {
                var len = window.localStorage.length;
                var arr = [];
                for (var i = 0; i < len; i++) {
                    // 调用key方法获取localStorage中数据对应的键名
                    var getKey = localStorage.key(i);
                    //var getVal = localStorage.getItem(getKey);
                    arr.push(getKey);
                    console.log(getKey);
                }
                console.log(arr);
                for(var k=0;k 
  

 

你可能感兴趣的:(cookie和localStorage常用方法)