客户端储存的数据:
localStorage-用于长久保存整个网站的数据,保存的数据需要手动删除,没有过期时间
sessionStorage-用于临时保存在同一窗口下,关闭窗口,数据自行删除。
完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>阅读存储</title>
</head>
<body onload="clickCounter()">
<div id="result"></div>
</body>
<script>
function clickCounter() {
if (typeof(Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
}
else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = " 阅读量:" + localStorage.clickcount;
}
else {
document.getElementById("result").innerHTML = "对不起,您的浏览器不支持 web 存储。";
}
}
</script>
</html>