国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

HTML5 完整版手冊(cè) / HTML5 Web 存儲(chǔ)

HTML5 Web 存儲(chǔ)


HTML5 web 存儲(chǔ),一個(gè)比cookie更好的本地存儲(chǔ)方式。


什么是 HTML5 Web 存儲(chǔ)?

使用HTML5可以在本地存儲(chǔ)用戶的瀏覽數(shù)據(jù)。

早些時(shí)候,本地存儲(chǔ)使用的是cookies。但是Web 存儲(chǔ)需要更加的安全與快速. 這些數(shù)據(jù)不會(huì)被保存在服務(wù)器上,但是這些數(shù)據(jù)只用于用戶請(qǐng)求網(wǎng)站數(shù)據(jù)上.它也可以存儲(chǔ)大量的數(shù)據(jù),而不影響網(wǎng)站的性能.

數(shù)據(jù)以 鍵/值 對(duì)存在, web網(wǎng)頁(yè)的數(shù)據(jù)只允許該網(wǎng)頁(yè)訪問(wèn)使用。


瀏覽器支持

Internet Explorer

Internet Explorer 8+, Firefox, Opera, Chrome, 和 Safari支持Web 存儲(chǔ)。

注意: Internet Explorer 7 及更早IE版本不支持web 存儲(chǔ).


localStorage 和 sessionStorage 

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

  • localStorage - 沒(méi)有時(shí)間限制的數(shù)據(jù)存儲(chǔ)

  • sessionStorage - 針對(duì)一個(gè) session 的數(shù)據(jù)存儲(chǔ)

在使用 web 存儲(chǔ)前,應(yīng)檢查瀏覽器是否支持 localStorage 和sessionStorage:

if(typeof(Storage)!=="undefined")
  {
  // Yes! localStorage and sessionStorage support!
  // Some code.....
  }
else
  {
  // Sorry! No web storage support..
  }



localStorage 對(duì)象

localStorage 對(duì)象存儲(chǔ)的數(shù)據(jù)沒(méi)有時(shí)間限制。第二天、第二周或下一年之后,數(shù)據(jù)依然可用。

實(shí)例

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文網(wǎng)(php.cn)</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>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例解析:

  • 使用  key="lastname" 和value="Smith" 創(chuàng)建一個(gè) localStorage 鍵/值對(duì)

  • 檢索鍵值為"lastname" 的值然后將數(shù)據(jù)插入 id="result"的元素中

提示: 鍵/值對(duì)通常以字符串存儲(chǔ),你可以按自己的需要轉(zhuǎn)換該格式。

下面的實(shí)例展示了用戶點(diǎn)擊按鈕的次數(shù). 代碼中的字符串值轉(zhuǎn)換為數(shù)字類(lèi)型:

實(shí)例

<!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>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例



sessionStorage 對(duì)象

sessionStorage 方法針對(duì)一個(gè) session 進(jìn)行數(shù)據(jù)存儲(chǔ)。當(dāng)用戶關(guān)閉瀏覽器窗口后,數(shù)據(jù)會(huì)被刪除。

如何創(chuàng)建并訪問(wèn)一個(gè) sessionStorage::

實(shí)例

<!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>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例