webstorage 分sessionStorage和localstorage,sessionStorage是暫時保存,localStorage是永久保存。 sessionStorage假設瀏覽器關閉了,數據就沒有了。而localStorage則不會。html
演示樣例web
<span style="font-family:Microsoft YaHei;font-size:18px;"><!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script language="JavaScript"> function saveStorage(id){ var target = document.getElementById(id); var str = target.value; sessionStorage.setItem("msg", str); } function loadStorage(id){ var target = document.getElementById(id); var msg = sessionStorage.getItem("msg"); target.innerHTML = msg; } </script> </head> <body> <p id="msg"></p> <input type="text" id="input"/> <button onclick="saveStorage('input')">保存數據</button> <button onclick="loadStorage('msg')">獲取數據</button> </body> </html></span>