通过一枚页面计数器来区别localStorage与sessionStorage。
通过一个计数变量pageconut,每刷新页面,增加的是localStorage的数量,而sessionStorage只记录当前访问数量;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>页面访问数</title>
</head>
<body>
<script>
if(localStorage.pagecount){
localStorage.pagecount = Number(localStorage.pagecount) + 1;
}else{
localStorage.pagecount = 1;
}
document.write('总访问数:' + localStorage.pagecount);
if(sessionStorage.pagecount){
sessionStorage.pagecount = Number(sessionStorage.pagecount) + 1;
}else{
sessionStorage.pagecount = 1;
}
document.write('当前访问数:' + sessionStorage.pagecount);
</script>
</body>
</html>