Example Enter value: Session storage save Clear session storage Local storage save Clear local storage
Code "use strict"; function displayStoredValues() { let strSession = "Session storage: "; let strLocal = "Local storage: "; if (sessionStorage) { if (sessionStorage.getItem('name') != null) { strSession += sessionStorage.getItem('name'); } } else { strSession += "Not available"; } if (localStorage) { if (localStorage.getItem('name') != null) { strLocal += localStorage.getItem('name'); } } else { strLocal += "Not available"; } document.getElementById('session').innerHTML = strSession; document.getElementById('local').innerHTML = strLocal; document.getElementById('name').value = ""; } function saveSession() { if (!sessionStorage) { alert("Your browser does not support session storage"); } else { let elem = document.getElementById('name'); sessionStorage.setItem('name', elem.value); displayStoredValues(); } return false; } function saveLocal() { if (!localStorage) { alert("Your browser does not support local storage"); } else { let elem = document.getElementById('name'); localStorage.setItem('name', elem.value); displayStoredValues(); } return false; } function clearLocalStorage() { if (localStorage) localStorage.clear(); displayStoredValues(); return false; } function clearSessionStorage() { if (sessionStorage) sessionStorage.clear(); displayStoredValues(); return false; } window.onload = function() { displayStoredValues(); document.getElementById('btnSaveSession').onclick = saveSession; document.getElementById('btnSaveLocal').onclick = saveLocal; document.getElementById('btnClearSession').onclick = clearSessionStorage; document.getElementById('btnClearLocal').onclick = clearLocalStorage; };