您好,登錄后才能下訂單哦!
什么是localStorage
HTML5加入了localStorage,作為客戶本地存儲(chǔ)文件,之前用cookie,但是每條cookie空間為4K localStorage 支持5M
一張圖了解下客戶端瀏覽器的存儲(chǔ)發(fā)展史
瀏覽器支持情況
Feature Chrome Firefox Internet Explorer Opera Safari Android Opera Mobile Safari Mobile
localStorage 4 3.5 8 10.50 4 2.1 11 iOS 3.2
sessionStorage 5 2 8 10.50 4 2.1 11 iOS 3.2
特點(diǎn)
判斷l(xiāng)ocalStorage
if(!window.localStorage){
alert("瀏覽器支持localstorage");
return false;
}else{
//開始浪
}
HTML5 localStorage 增刪改遍歷
var storage=window.localStorage;
storage.setItem("name","tom");
官方不太推薦,但是支持的寫法
storage["name"]="tom";
storage.name="tom";
var a1=storage.getItem("name");
官方不太推薦,但是支持的寫法
var a2=storage.name;
var a3=storage["name"];
更新?html5的localStorage 存的時(shí)候是單鍵值對(duì) key-value模式,重新寫一遍name就是更新了
storage.setItem("name","Jack");
storage.removeItem("name");
var storage=window.localStorage;
storage.setItem("name1","tom");
storage.setItem("name2","jack");
storage.setItem("name3","Lucy");
for(var i=0;i<storage.length;i++){
var key = storage.key(i);
var value = storage.getItem(key);
console.log(key + " - " + value);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>測(cè)試localStorage</title>
</head>
<script>
if(!window.localStorage){
alert("瀏覽器不支持localstorage");
}else{
var storage=window.localStorage;
storage.setItem("a","tom");
console.log(typeof storage["a"]);
//第一種方法讀取
var a1=storage.getItem("a");
console.log(a1);
}
</script>
<body>
</body>
</html>
https://java-er.com/blog/html5-localstorage-all
MORE================
html5 localStorage緩存音樂和視頻
H5 localStorage 緩存圖片
H5 本地緩存localStorage緩存計(jì)數(shù)器
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。