Created
March 28, 2018 02:34
-
-
Save a1exlism/a20d08391783f98e9e79979de41d76d8 to your computer and use it in GitHub Desktop.
sessionStorage and localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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