当前位置:首页 > HTML5教程 > HTML5入门

HTML5Web存储

html5 web 存储

html5 web 存储,一个比cookie更好的本地存储方式。

什么是 html5 web 存储?

使用html5可以在本地存储用户的浏览数据。

早些时候,本地存储使用的是cookies。但是web 存储需要更加的安全与快速. 这些数据不会被保存在服务器上,但是这些数据只用于用户请求网站数据上.它也可以存储大量的数据,而不影响网站的性能.

数据以 键/值 对存在, web网页的数据只允许该网页访问使用。

浏览器支持

internet explorer 8+, firefox, opera, chrome, 和 safari支持web 存储。

注意: internet explorer 7 及更早ie版本不支持web 存储.

localstorage 和 sessionstorage 

there are two new objects for storing data on the client:

  • localstorage - 没有时间限制的数据存储

  • sessionstorage - 针对一个 session 的数据存储

在使用 web 存储前,应检查浏览器是否支持 localstorage 和sessionstorage:

if(typeof(storage)!=="undefined")
  {
  // yes! localstorage and sessionstorage support!
  // some code.....
  }
else
  {
  // sorry! no web storage support..
  }

 

localstorage 对象

localstorage 对象存储的数据没有时间限制。第二天、第二周或下一年之后,数据依然可用。

实例

<!doctype html>
<html>
<head> 
<meta charset="utf-8"> 
<title>无忧网</title> 
</head>
<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>
 实例解析:
  • 使用  key="lastname" 和value="smith" 创建一个 localstorage 键/值对

  • 检索键值为"lastname" 的值然后将数据插入 id="result"的元素中

提示: 键/值对通常以字符串存储,你可以按自己的需要转换该格式。

下面的实例展示了用户点击按钮的次数. 代码中的字符串值转换为数字类型:

实例

<!doctype html>
<html>
<head>
<script>
function clickcounter()
{
if(typeof(storage)!=="undefined")
  {
  if (localstorage.clickcount)
    {
    localstorage.clickcount=number(localstorage.clickcount)+1;
    }
  else
    {
    localstorage.clickcount=1;
    }
  document.getelementbyid("result").innerhtml="you have clicked the button " + localstorage.clickcount + " time(s).";
  }
else
  {
  document.getelementbyid("result").innerhtml="sorry, your browser does not support web storage...";
  }
}
</script>
</head>
<body>
<p><button onclick="clickcounter()" 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 will continue to count (is not reset).</p>
</body>
</html>

 

 

sessionstorage 对象

sessionstorage 方法针对一个 session 进行数据存储。当用户关闭浏览器窗口后,数据会被删除。

如何创建并访问一个 sessionstorage::

实例

<!doctype html>
<html>
<head>
<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 onclick="clickcounter()" 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>


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!