<!DOCTYPE html> <html> <body> <script> // validate thether this browser support storage if(window.localStorage){ console.log("support local storage"); } if(window.sessionStorage){ console.log("support session storage"); } addEntryForSessionStorage(); addEntryForLocalStorage(); printAllLocalStoreInfo(); printAllSessionStoreInfo(); /* ============================== session storage ============================== */ function addEntryForSessionStorage(){ var key = window.sessionStorage.length+1; var value = parseInt(Math.random()*1000); window.sessionStorage.setItem(key,value); } function printAllSessionStoreInfo(){ console.log("all session storage informaton is below."); var sessionStorage = window.sessionStorage; for(var i=0;i<sessionStorage.length;i++){ var key = sessionStorage.key(i); var value = sessionStorage.getItem(key); console.log(key+":"+value); } } /* ============================== local storage ============================== */ function addEntryForLocalStorage(){ var key = window.localStorage.length+1; var value = parseInt(Math.random()*1000); window.localStorage.setItem(key,value); } function printAllLocalStoreInfo(){ console.log("all local storage informaton is below."); var localStorage = window.localStorage; for(var i=0;i<localStorage.length;i++){ var key = localStorage.key(i); var value = localStorage.getItem(key); console.log(key+":"+value); } } </script> </body> </html>
时间: 2024-10-10 13:41:56