Log Local and Session Storage in Console


/ Published in: JavaScript
Save to your folder(s)

logs values for local/session storage if supported


Copy this code and paste it in your HTML
  1. var output = "LOCALSTORAGE DATA:\n------------------------------------\n";
  2. if (localStorage) {
  3. if (localStorage.length) {
  4. for (var i = 0; i < localStorage.length; i++) {
  5. output += localStorage.key(i) + ': ' + localStorage.getItem(localStorage.key(i)) + '\n';
  6. }
  7. } else {
  8. output += 'There is no data stored for this domain.' + '\n';
  9. }
  10. } else {
  11. output += 'Your browser does not support local storage. + '\n';
  12. }
  13. output += "SESSIONSTORAGE DATA:\n-------------------------------------\n";
  14. if (sessionStorage) {
  15. if (sessionStorage.length) {
  16. for (var i = 0; i < sessionStorage.length; i++) {
  17. output += sessionStorage.key(i) + ': ' + sessionStorage.getItem(sessionStorage.key(i)) + '\n';
  18. }
  19. } else {
  20. output += 'There is no data stored for this page.' + '\n';
  21. }
  22. } else {
  23. output += 'Your browser does not support session storage.' + '\n';
  24. }
  25. console.log(output);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.