JavaScript系列之客户端存储

localStorage和sessionStorage支持的方法
setItem(key,value),getItem(key),removeItem(key),clear(),存储事件(storage)
使用key()和length,能够枚举全部存储数据的名字html

for(var i=0;l<localStorage.length;i++){
    var name = localStorage.key(i)
    var value = localStorage.getItem(name)
}

当存储数据发生变化时,触发存储事件(storage)web

document.addEventListener('storage', () => {
    // When local storage changes, dump the list to
    // the console.
    console.log(JSON.parse(window.localStorage.getItem('sampleList')));    
});
window.onstorage = () => {
    // When local storage changes, dump the list to
    // the console.
    console.log(JSON.parse(window.localStorage.getItem('sampleList')));    
};

cookie数据会自动在web浏览器和web服务器之间传输的。
name,value,path,domain,max-age
若是没有为一个cookie设置域属性,那么domain属性的默认值是当前web服务器的主机名。cookie的域只能设置为当前服务器的域。
secure为true是,只能经过https或者其余安全的协议链接的时候传递cookie。
保存cookie,name=value;max-age=seconds;path=path;domain=domain;secure
要改变cookie的值,须要使用相同的名字、路径和域,可是新的值从新设置cookie的值。
要删除一个cookie,须要使用相同的名字、路径和域,而后指定一个任意(非空)的值,而且将max-age属性指定为0,再次设置cookie。
单个cookie大小有4KB的限制。每一个web服务器存储不超过20个cookie,浏览器存储不超过300个cookie浏览器

window.document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)')

manifest文件缓存

CACHE MANIFEST
#下面的内容是应用程序依赖的资源文件的URL
CACHE:
myapp.html
myapp.js

#清单每行包含两个URL
FALLBACK:
vidoes/ offline_help.html

#URL中的资源从不缓存,总要经过网络获取
NETWORK:
cgi/

应用程序缓存清单文件约定以.appcache做为文件扩展名。安全

缓存的更新
浏览器只检查清单文件,而不会去检查缓存的文件是否有更新。
浏览器在更新缓存过程当中会触发一系列事件,能够经过注册处理程序来跟踪这个过程同时提供反馈给用户。
该事件处理程序是注册在ApplicationCache对象上,该对象是Window的applicationCache属性的值。
checking——noupdate(downloading更新或者首次下载)——progress——updateready(cached)
——error(离线状态)
——obsolete(引用一个不存在的清单文件,应用将被从缓存中移除)
update()
swapCache()服务器

相关文章
相关标签/搜索