UNPKG

1.68 kBHTMLView Raw
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
22 // What if the value is not a string?
23 o = {a:1, b:'hello', c:{x: 1, y: new Date()}};
24 console.log(o.toString());
25 s = 'hello'
26 console.log(s.toString());
27 localStorage.setItem('1', o);
28 console.log(typeof(localStorage.getItem('1')));
29 a = [1, 'some string', {a:1, b:'some string', c:{x: 1, y: 2}}]
30 console.log(a.toString());
31 localStorage.setItem('2', a);
32 console.log(typeof(localStorage.getItem('2')));
33 // After running the above in Firefox, Safari, and Chrome, they all seem to do a .toString() on the value
34
35 // What if the key is not a string?
36 localStorage.setItem(99, 'hello Number')
37 localStorage.setItem(o, 'hello Object')
38 localStorage.setItem(a, 'hello Array')
39 // After running the above in all 3 browsers, I can confirm that this also seems to do a .toString() on the key
40
41 displayLocalStorageData();
42 </script>
43
44 </head>
45 <body>
46
47 </body>
48</html>