HTML5 Web 存儲



HTML5 web 儲存,一個比cookie更好的本地儲存方式。


什麼是 HTML5 Web 儲存?

使用HTML5可以在本機儲存使用者的瀏覽資料。

早期,本地儲存使用的是cookies。但是Web 儲存需要更加的安全與快速. 這些數據不會被保存在伺服器上,但是這些數據只用於用戶請求網站數據上.它也可以儲存大量的數據,而不影響網站的性能.

資料以鍵/值對存在, web網頁的資料只允許該網頁存取使用。


瀏覽器支援

Internet Explorer

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>php中文网(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>

運行實例»點擊"運行實例" 按鈕查看線上實例

實例解析:

  • 使用 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>

##運行實例»##點擊"運行實例" 按鈕查看線上實例

#