html5緩存
阿新 • • 發佈:2017-06-05
鍵值 設置 oct 鍵值對 write oca javascrip 格式 使用
HTML5 提供了兩種在client存儲數據的新方法:
localStorage - 沒有時間限制的數據存儲
sessionStorage - 針對一個 session 的數據存儲
這些都是由 cookie 完畢的。 localStorage.a = 3;//設置a為"3"
localStorage["a"] = "sfsf";//設置a為"sfsf",覆蓋上面的值
localStorage.setItem("b","isaac");//設置b為"isaac"
var a1 = localStorage["a"];//獲取a的值
var a2 = localStorage.a;//獲取a的值
var b = localStorage.getItem("b");//獲取b的值
localStorage.removeItem("c");//清除c的值
這裏最推薦使用的自然是getItem()和setItem()。清除鍵值對使用removeItem()。
假設希望一次性清除全部的鍵值對,能夠使用clear()。
localStorage - 沒有時間限制的數據存儲
sessionStorage - 針對一個 session 的數據存儲
這些都是由 cookie 完畢的。
可是 cookie 不適合大量數據的存儲,
以下一起來看一個localStorage 存儲json對象存儲格式問題的解決的方法
<!DOCTYPE HTML> <html> <body> <script type="text/javascript"> var obj= [{ "name": "a張三", "addr":"天津"}, { "name": "a張狗","addr":"上海"}, { "name": "a王武","addr":"北京"}, ]; obj=JSON.stringify(obj); //轉化為json字符串 localStorage.setItem("temp2",obj); //返回字符串 //typeof localStorage.getItem("temp2"); //返回字符串轉化為json對象 obj=JSON.parse(localStorage.getItem("temp2")); document.write(obj[0].name); </script> </body> </html>
localStorage["a"] = "sfsf";//設置a為"sfsf",覆蓋上面的值
localStorage.setItem("b","isaac");//設置b為"isaac"
var a1 = localStorage["a"];//獲取a的值
var a2 = localStorage.a;//獲取a的值
var b = localStorage.getItem("b");//獲取b的值
localStorage.removeItem("c");//清除c的值
這裏最推薦使用的自然是getItem()和setItem()。清除鍵值對使用removeItem()。
假設希望一次性清除全部的鍵值對,能夠使用clear()。
html5緩存