localStorage 实例:
<!DOCTYPE> <html> <head> <title>test2.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript"> function submitL(obj){ var lname=document.getElementById("logname"); if(lname.value=='test'){ localStorage.lname=lname.value; } var ln=document.getElementById("msg"); ln.innerHTML=localStorage.lname; } </script> </head> <body> 页面计数器<br /> localStorage <br/> <input id="logname" value="" /> <input type="button" value="submit" onclick="submitL(this);"> <div id="msg"> </div> <div id="numcount"> </div> <script type="text/javascript"> if(localStorage.pagecount){ localStorage.pagecount=Number(localStorage.pagecount)+1; }else{ localStorage.pagecount=1; } document.write("Visit: "+localStorage.pagecount+" times"); </script> </body> </html>
http://www.w3school.com.cn上说localStorage 方法存储的数据没有时间限制。第二天、第二周或下一年之后,数据依然可用。本人小小测试了一下,当我们将数据存储下来后,关闭浏览器再次打开数据还能取出来,但是当你清除掉cookie时它就木有啦,木有啦!这个实际上还是和cookie相关的。其次,本人还发现一个问题是,从history访问页面时,计数器同样会计数。