localStorage的增删改查都其实比较简单,每次需要判断浏览器是否禁用,这挺麻烦的;
而且没有像COOKIE一样的,自动过期时间,长期下去会有垃圾数据在里面;我们知道android的系统只支持5M存储空间,iphone是10MB,一旦超过限制会提示用户,甚至报错。
一、包含一下功能:
1、定时清理localStorage;
2、支持过期时间;
3、检测浏览器是否禁用localStorage;
二、顺便贴一个COOKIE的存取方法,接口类似:
(function($){ /* * $.cookie('name', 'test',{expires: 7}); //设置 * * */ $.cookie = function(name, value, options) { if (typeof value != 'undefined') { options = options || {}; if (value === null) { value = ''; options = $.extend({}, options); options.expires = -1; } var expires = ''; if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { var date; if (typeof options.expires == 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); } var path = options.path ? '; path=' + (options.path) : ''; var domain = options.domain ? '; domain=' + (options.domain) : ''; var secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); return this; } else { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i].trim(); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } }; /* * * $.localStorage('test') //get * $.localStorage('test',123) //set * $.localStorage('test',{value:123}) //set * $.localStorage('test',{value:123}, 1) //set * $.localStorage('test',null) //remove * */ var isCache=true, minutes = 1000*60, now = Date.now(); try { localStorage.setItem('cache','test'); } catch (e) { isCache= false; } resetCache = function(time){//定时清理 if(!isCache) return ; var expires, day= minutes*60*24; time = time || 0; if((expires=localStorage.getItem('_expires')) && expires>now){ return false; } var len= localStorage.length,item,key,t; for(var i=0; i<len; i++){ key= localStorage.key(i); item=localStorage.getItem(key); if(item && item.indexOf('_expires')!=-1){ t=item.match(/_expires":(\d+)/)[1]; if(now<t){ continue; } } localStorage.removeItem(key); } return localStorage.setItem('_expires', day*time); } resetCache(60); //2个月检测一遍 $.localStorage = function(name, value, time) { if(!isCache) return false; if (typeof value != 'undefined') { //set if(value===null){ return localStorage.removeItem(name); } if(!isNaN(+time)){ value = {value: value, _expires : now+time*minutes}; } localStorage.setItem(name,JSON.stringify(value)); return value.value || value; }else{ //get var localValue = null,st,et; localValue = localStorage.getItem(name); try { localValue = JSON.parse(localValue); } catch (e) { return localValue; } if($.isObject(localValue) && (et=localValue._expires) ){ if(now > et){ localStorage.removeItem(name); localValue=null; }else{ localValue = localValue['value']; } } return localValue; } }; })(Zepto);