-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlocalStorage-object.html
More file actions
41 lines (33 loc) · 1.02 KB
/
localStorage-object.html
File metadata and controls
41 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html>
<head>
<title>Local Storage</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
//give me a random number from 0-100
var randomNumber = Math.floor(Math.random()*100);
//give me a random number from 0 - 0.999999999
var randomValue = Math.random();
//try to get the stored object
var numbers = localStorage.getItem("numbers");
if(numbers === null){
//if it doesn't exist, create it
console.log("not an array, creating one");
numbers = {};
} else {
//if it exists, convert the string to an object
console.log("numbers is an object");
numbers = JSON.parse(numbers);
}
//add a random key, and the random value to it
numbers[randomNumber] = randomValue;
//convert the object into a string, and store it
localStorage.setItem("numbers", JSON.stringify(numbers));
console.log(numbers);
});
</script>
</head>
<body>
</body>
</html>