日期:2014-05-17 浏览次数:20754 次
<!DOCTYPE HTML> <html> <head> <title>HTML5 Web Storage</title> </head> <body> <h1>HTML5 Web Storage</h1> <button type="button" onclick="write_local_storage()">Write Local Storage</button> <button type="button" onclick="read_local_storage()">Read Local Storage</button> <button type="button" onclick="write_session_storage()">Write Session Storage</button> <button type="button" onclick="read_session_storage()">Read Session Storage</button> <button type="button" onclick="clear_data()">Clear</button> </body> </html>
function check_support() { if(typeof(Storage) == "undefined") { alert("Sorry! No web storage support!"); return false; } return true; }
function write_local_storage() { if (check_support()) { var persistObject = { 'Smith': 23, 'Kobi': 18, 'Alex': 32 }; localStorage.setItem('persist_data', JSON.stringify(persistObject)); }
function read_local_storage() { if (check_support()) { var storage = window.localStorage; var retrievedObject = storage.getItem('persist_data'); var length = storage.length; alert("Retrieving " + length + " item(s) from localstroage"); alert(retrievedObject); } }
function write_session_storage() { if (check_support()) { var sessionObject = { 'keycode': 111, 'passpharse': 222, 'id': 888 }; // Put the object into storage sessionStorage.setItem('session_data', JSON.stringify(sessionObject)); } }
function read_session_storage() { if (check_support()) { // Retrieve the object from storage var storage = window.sessionStorage; var retrievedObject = storage.getItem('session_data'); var length = storage.length; alert("Retrieving " + length + " item(s) from localstroage"); alert(retrievedObject); } }