HTML5:
<1> localStorage 没有时间限制
<2> sessionStorage 针对session数据存储, 浏览器关闭则失效
cookie缺点: cookie不适合大量数据存储
<1> localStorage
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>localStorage</title> <script src="myjavascript.js"></script> </head> <body> <textarea id="ta" style="width:200px; height: 200px"></textarea> <button id="btn">save</button> </body> </html>
var ta; var button; window.onload = function(){ ta = document.getElementById("ta"); if(localStorage.text) ta.value = localStorage.text; button = document.getElementById("btn"); button.onclick = function () { localStorage.text = ta.value; } }
点击save按钮之后, 刷新页面, 甚至关闭浏览器再打开此页面都会显示mirror
2. sessionStorage
跟上面类似
时间: 2024-11-03 21:20:14