html 5中localstorage小结
html 5中的localstorage的小结 demo,如下:
1)
<!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 storagesessionStorage.setItem('session_data', JSON.stringify(sessionObject));}}
function read_session_storage(){if (check_support()){// Retrieve the object from storagevar storage = window.sessionStorage; var retrievedObject = storage.getItem('session_data');var length = storage.length;alert("Retrieving " + length + " item(s) from localstroage");alert(retrievedObject);}}