test.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <script type="text/javascript">
  6. function displayLocalStorageData() {
  7. var output = "LOCALSTORAGE DATA:\n------------------------------------\n";
  8. if (localStorage) {
  9. if (localStorage.length) {
  10. for (var i = 0; i < localStorage.length; i++) {
  11. output += localStorage.key(i) + ': ' + localStorage.getItem(localStorage.key(i)) + '\n';
  12. }
  13. } else {
  14. output += 'There is no data stored for this domain.';
  15. }
  16. } else {
  17. output += 'Your browser does not support local storage.'
  18. }
  19. console.log(output);
  20. }
  21. // What if the value is not a string?
  22. o = {a:1, b:'hello', c:{x: 1, y: new Date()}};
  23. console.log(o.toString());
  24. s = 'hello'
  25. console.log(s.toString());
  26. localStorage.setItem('1', o);
  27. console.log(typeof(localStorage.getItem('1')));
  28. a = [1, 'some string', {a:1, b:'some string', c:{x: 1, y: 2}}]
  29. console.log(a.toString());
  30. localStorage.setItem('2', a);
  31. console.log(typeof(localStorage.getItem('2')));
  32. // After running the above in Firefox, Safari, and Chrome, they all seem to do a .toString() on the value
  33. // What if the key is not a string?
  34. localStorage.setItem(99, 'hello Number')
  35. localStorage.setItem(o, 'hello Object')
  36. localStorage.setItem(a, 'hello Array')
  37. // After running the above in all 3 browsers, I can confirm that this also seems to do a .toString() on the key
  38. displayLocalStorageData();
  39. </script>
  40. </head>
  41. <body>
  42. </body>
  43. </html>