web local storage
var storage = { save2Local:function (key,val) { localStorage.setItem(key,val); }, restore4Local:function(key) { if (window.localStorage) { return localStorage.getItem(key); } }, /*** * clear localStorage */ removeLocalStorage:function (key) { if(key){ localStorage.removeItem(key); } } };
application:
var currentDateTime=new Date(); var date_format='%Y%m%d'; var currentDate=currentDateTime.format2(date_format);//20170316 console.log('currentDate:'+currentDate); // cache to browser var browserCache=function (prefix) { var answer=$('textarea[name=content]').val(); if(answer){//Every currentDate needs to be regenerated currentDate=new Date().format2(date_format);//20170316 storage.save2Local(prefix+currentDate,answer); } }; // get cache from browser 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(); }