Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Created March 28, 2018 02:34
Show Gist options
  • Save a1exlism/a20d08391783f98e9e79979de41d76d8 to your computer and use it in GitHub Desktop.
Save a1exlism/a20d08391783f98e9e79979de41d76d8 to your computer and use it in GitHub Desktop.
sessionStorage and localStorage
// Refer Link: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
const STATE = `sessionStorage and localStorage are both window object's inner properties`;
function checkStorage(type) {
let storage = window[type];
let x = '__storage_test__';
try {
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch(e) {
return false;
// restrict demo is in the top link
}
}
localStorage.setItem('foo', 'bar');
localStorage.getItem('foo');
localStorage.removeItem('foo');
localStorage.clear(); // delete all
// reaction when storageChange
window.addEventListener('storage', function() {
// codes
})
const STATE2 = `same as sessionStorage`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment