CIS 119 - Web storage (DOM storage)

Objectives

Overview

Using sessionStorage and localStorage

Storage demonstration code

See the live storage demonstration. This example allows you to store and display a name value in either local or session storage.

<!DOCTYPE html> <html lang="en"> <head> <title>WebStorage</title> <meta charset="utf-8"> <script> function saveSession() { if (!sessionStorage) { alert("Your browser does not support session storage"); } else { var elem = document.getElementById('name'); sessionStorage.setItem('name', elem.value); document.getElementById('result').innerHTML = elem.value; } return false; } function saveLocal() { if (!localStorage) { alert("Your browser does not support local storage"); } else { var elem = document.getElementById('name'); localStorage.setItem('name', elem.value); document.getElementById('result').innerHTML = elem.value; } return false; } function clearStorage() { if (localStorage) localStorage.clear(); if (sessionStorage) sessionStorage.clear(); document.getElementById('result').innerHTML = ""; return false; } window.onload = function() { var found = false; var value = null; if (sessionStorage) value = sessionStorage.getItem('name'); if (value == null && localStorage) value = localStorage.getItem('name'); document.getElementById('result').innerHTML = value; }; </script> </head> <body> <div>The stored value is <span id="result"></span></div> <form> <p><label for="name">Name</label> <input type="text" id="name"></p> <p><input type="submit" value="Session storage save" onclick="return saveSession();"></p> <p><input type="submit" value="Local storage save" onclick="return saveLocal();"></p> <p><input type="submit" value="Clear storage" onclick="return clearStorage();"></p> </form> </body> </html>

More storage demonstration code

See the additional storage demonstration. This example allows you to store and display multiple key/value pairs of your choosing.

<!DOCTYPE html> <html lang="en"> <head> <title>Web Storage Demo 2</title> <meta charset="utf-8"> <style> #results { font-family: monospace; whitespace: pre; } label { display: inline-block; width: 4em; } </style> <script> function $(id) { return document.getElementById(id); } function clearTextBoxes() { $('value').value = ""; $('key').value = ""; } function saveSession() { if (!sessionStorage) { alert("Your browser does not support session web storage"); } else { var key = $('key').value; var value = $('value').value; if (key && value) sessionStorage.setItem(key, value); else alert("Either the key or value (or both) were missing."); window.location.reload(); } clearTextBoxes(); return false; } function saveLocal() { if (!localStorage) { alert("Your browser does not support local web storage"); } else { var key = $('key').value; var value = $('value').value; if (key && value) localStorage.setItem(key, value); else alert("Either the key or value (or both) were missing."); window.location.reload(); } clearTextBoxes(); return false; } function clearStorage() { if (localStorage) localStorage.clear(); if (sessionStorage) sessionStorage.clear(); window.location.reload(); return false; } window.onload = function() { clearTextBoxes(); $('btnSaveLocal').onclick = saveLocal; $('btnSaveSession').onclick = saveSession; $('btnClear').onclick = clearStorage; var msg = ""; var i = 0; if (sessionStorage) { var ssLength = sessionStorage.length; if (ssLength > 0) { msg += '<h3>Session storage</h3>'; msg += '<ol type="1">'; for (i=0; i<ssLength; i++) { var ssKey = sessionStorage.key(i); msg += '<li>' + ssKey + ' = ' + sessionStorage.getItem(ssKey) + '</li>'; } msg += '</ol>'; } } if (localStorage) { var lsLength = localStorage.length; if (lsLength > 0) { msg += '<h3>Local storage</h3>'; msg += '<ol type="1">'; for (i=0; i<lsLength; i++) { var lsKey = localStorage.key(i); msg += '<li>' + lsKey + ' = ' + localStorage.getItem(lsKey) + '</li>'; } msg += '</ol>'; } } if (!localStorage && !sessionStorage) $('results').innerHTML = '<h3>Web storage not available.</h3>'; else if (msg == "") $('results').innerHTML = '<h3>No data found in web storage.</h3>'; else $('results').innerHTML = msg; }; </script> </head> <body> <div id="results"></div> <form> <p><label for="key">Key: </label><input type="text" id="key"></p> <p><label for="value">Value: </label><input type="text" id="value"></p> <p><button type="button" id="btnSaveSession">Session storage save</button></p> <p><button type="button" id="btnSaveLocal">Local storage save</button></p> <p><button type="button" id="btnClear">Clear storage</button></p> </form> </body> </html>