HTML5 Web 存儲

HTML5 Web 儲存

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

什麼是 HTML5 Web 儲存?

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

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

資料以鍵/值對存在, 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"> 
</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">点我</button></p>
<div id="result"></div>
<p>单击该按钮查看计数器增加.</p>
<p>关闭浏览器选项卡(或窗口),再试一次,计数器将继续计数(不是重置)。</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>单击该按钮查看计数器增加.</p>
<p>关闭浏览器选项卡(或窗口),再试一次,计数器将继续计数(不是重置)。</p>
</body>
</html>


繼續學習
||
<!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">点我</button></p> <div id="result"></div> <p>单击该按钮查看计数器增加.</p> <p>关闭浏览器选项卡(或窗口),再试一次,计数器将继续计数(不是重置)。</p> </body> </html>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!