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()服務器

相關文章
相關標籤/搜索