HTML5中sessionStorage和localStorage兩種儲存方式的使用
<head> <meta charset="utf-8"> <title>HTML5 本地儲存</title> <script type="text/javascript"> function $(id){return document.getElementById(id);} function savesessionStorage(id){sessionStorage.setItem('message',$(id).value);} function loadsessionStorage(id){$(id).innerHTML=sessionStorage.getItem("message");} function savelocalStorage(id){localStorage.setItem('message',$(id).value);} function loadlocalStorage(id){$(id).innerHTML=localStorage.getItem("message");} </script> </head>
<body> <div> <h2>sessionStorage 儲存</h2> <p id="sessionMsg"></p> <input type="text" id="sessionInput"/> <input type="button" value="儲存資料" onclick="savesessionStorage('sessionInput');"/> <input type="button" value="讀取資料" onclick="loadsessionStorage('sessionMsg');"/>
<br/> <h2>localStorage 儲存</h2> <p id="localMsg"></p> <input type="text" id="localInput"/> <input type="button" value="儲存資料" onclick="savelocalStorage('localInput');"/> <input type="button" value="讀取資料" onclick="loadlocalStorage('localMsg');"/> </div> </body>