码迷,mamicode.com
首页 > 其他好文 > 详细

localStorage sessionStorage

时间:2016-05-13 05:03:37      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:localstorage sessionstorage html5

localStorage存储在本地关闭浏览器后还存在 sessionStorage关闭浏览器后不再存在

<!DOCTYPE html>
<html>
<head>
<meta content="text/html" http-equiv="content-type" charset="utf-8"/>
<body>

<div id="result"></div>
<script>
if(typeof(Storage)!=="undefined")
  {
  localStorage.lastname="Smith";
  document.getElementById("result").innerHTML="Last name: " + localStorage.lastname;
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support web storage...";
  }
</script>

</body>
</html>


<!DOCTYPE html>
<html>
<head>
<meta content="text/html" http-equiv="content-type" charset="utf-8"/>
<script>
function clickCounter()
{
if(typeof(Storage)!=="undefined")
  {
  if (sessionStorage.clickcount)
    {
    sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
    }
  else
    {
    sessionStorage.clickcount=1;
    }
  document.getElementById("result").innerHTML="You have clicked the button " + sessionStorage.clickcount + " time(s) in this session.";
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support web storage...";
  }
}
</script>
</head>
<body>
<p><button type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter is reset.</p>
</body>
</html>

localStorage sessionStorage

标签:localstorage sessionstorage html5

原文地址:http://warmdoll.blog.51cto.com/10881334/1772653

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!