브라우저 로컬 캐시 localStorage

1652 단어 localStorage
자세히 보기
웹 로컬 저장소
var storage={
    save2Local:function (key,val) {
        localStorage.setItem(key,val);
    },
    restore4Local:function(key) {
    if (window.localStorage) {
        return localStorage.getItem(key);
    }
},
/***
 *  localStorage
 */
removeLocalStorage:function (key) {
    if(key){
        localStorage.removeItem(key);
    }
}

};

 
적용:
 var currentDateTime=new Date();
        var date_format='%Y%m%d';
        var currentDate=currentDateTime.format2(date_format);//20170316
        console.log('currentDate:'+currentDate);
        // 
        var browserCache=function (prefix) {
            var answer=$('textarea[name=content]').val();
            if(answer){// currentDate  
                currentDate=new Date().format2(date_format);//20170316
                storage.save2Local(prefix+currentDate,answer);
            }
        };
        // 
        var getbrowserCache=function (prefix) {
            var answer=storage.restore4Local(prefix+currentDate);
            if(!answer){
                currentDateTime.setDate(currentDateTime.getDate() - 1);
                currentDate=currentDateTime.format2(date_format);//20170316
                answer=storage.restore4Local(prefix+currentDate);
            }
            answer&&$('textarea[name=content]').val(answer).focus();
        }

 

좋은 웹페이지 즐겨찾기