html 5中localstorage小结
- - ITeye博客html 5中的localstorage的小结 demo,如下:. 2) 然后检查浏览器知否支持html 5的localstorage,比如:. 3) 写到localstorage,. 4) 读localstorage. 5 写session storage. 6 读session stage. 已有 0 人发表留言,猛击->> 这里<<-参与讨论.
<!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); } }