-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 20.js
154 lines (107 loc) · 5.58 KB
/
Day 20.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Activity 1: Understanding LocalStorage
//Task 1: Write a script to save a string value to `localStorage` and retrieve it. Log the retrieved value.
// Task 1
localStorage.setItem('myString', 'Hello, LocalStorage!');
let retrievedString = localStorage.getItem('myString');
console.log(retrievedString); // Output: Hello, LocalStorage!
//Task 2: Write a script to save an object to `localStorage` by converting it to a JSON string. Retrieve and parse the object, then log it
// Task 2
let myObject = { name: 'John Doe', email: '[email protected]' };
localStorage.setItem('myObject', JSON.stringify(myObject));
let retrievedObject = JSON.parse(localStorage.getItem('myObject'));
console.log(retrievedObject); // Output: { name: 'John Doe', email: '[email protected]' }
// Activity 2: Using LocalStorage
//Task 3: Create a simple form that saves user input (e.g., name and email) to `localStorage` when submitted. Retrieve and display the saved data on page load.
// <!-- HTML Form -->
// <form id="localStorageForm">
// <input type="text" id="name" placeholder="Name" required>
// <input type="email" id="email" placeholder="Email" required>
// <button type="submit">Save</button>
// </form>
// <div id="displayData"></div>
// <script>
// // Task 3
// document.getElementById('localStorageForm').addEventListener('submit', function(event) {
// event.preventDefault();
// let name = document.getElementById('name').value;
// let email = document.getElementById('email').value;
// let userData = { name: name, email: email };
// localStorage.setItem('userData', JSON.stringify(userData));
// displayData();
// });
// function displayData() {
// let userData = JSON.parse(localStorage.getItem('userData'));
// if (userData) {
// document.getElementById('displayData').innerText = `Name: ${userData.name}, Email: ${userData.email}`;
// }
// }
// window.onload = displayData;
// </script>
//Task 4: Write a script to remove an item from `localStorage`. Log the `localStorage` content before and after removal.
// Task 4
console.log('Before removal:', localStorage.getItem('userData')); // Log before removal
localStorage.removeItem('userData');
console.log('After removal:', localStorage.getItem('userData')); // Log after removal
// Activity 3: Understanding SessionStorage
//Task 5: Write a script to save a string value to `sessionStorage` and retrieve it. Log the retrieved value.
// Task 5
sessionStorage.setItem('mySessionString', 'Hello, SessionStorage!');
let retrievedSessionString = sessionStorage.getItem('mySessionString');
console.log(retrievedSessionString); // Output: Hello, SessionStorage!
//Task 6: Write a script to save an object to `sessionStorage` by converting it to a JSON string. Retrieve and parse the object, then log it.
// Task 6
let mySessionObject = { name: 'Jane Doe', email: '[email protected]' };
sessionStorage.setItem('mySessionObject', JSON.stringify(mySessionObject));
let retrievedSessionObject = JSON.parse(sessionStorage.getItem('mySessionObject'));
console.log(retrievedSessionObject); // Output: { name: 'Jane Doe', email: '[email protected]' }
// Activity 4: Using SessionStorage
//Task 7: Create a simple form that saves user input (e.g., name and email) to `sessionStorage` when submitted. Retrieve and display the saved data on page load.
// <!-- HTML Form -->
// <form id="sessionStorageForm">
// <input type="text" id="sessionName" placeholder="Name" required>
// <input type="email" id="sessionEmail" placeholder="Email" required>
// <button type="submit">Save</button>
// </form>
// <div id="sessionDisplayData"></div>
// <script>
// // Task 7
// document.getElementById('sessionStorageForm').addEventListener('submit', function(event) {
// event.preventDefault();
// let name = document.getElementById('sessionName').value;
// let email = document.getElementById('sessionEmail').value;
// let userData = { name: name, email: email };
// sessionStorage.setItem('sessionUserData', JSON.stringify(userData));
// displaySessionData();
// });
// function displaySessionData() {
// let userData = JSON.parse(sessionStorage.getItem('sessionUserData'));
// if (userData) {
// document.getElementById('sessionDisplayData').innerText = `Name: ${userData.name}, Email: ${userData.email}`;
// }
// }
// window.onload = displaySessionData;
// </script>
//Task 8: Write a script to remove an item from `sessionStorage`. Log the `sessionStorage` content before and after removal.
// Task 8
console.log('Before removal:', sessionStorage.getItem('sessionUserData')); // Log before removal
sessionStorage.removeItem('sessionUserData');
console.log('After removal:', sessionStorage.getItem('sessionUserData')); // Log after removal
//Activity 5: Comparing LocalStorage and SessionStorage
//Task 9: Write a function that accepts a key and a value, and saves the value to both `localStorage` and `sessionStorage`. Retrieve and log the values from both storage mechanisms.
// Task 9
function saveToBothStorages(key, value) {
localStorage.setItem(key, value);
sessionStorage.setItem(key, value);
console.log(`LocalStorage: ${localStorage.getItem(key)}`);
console.log(`SessionStorage: ${sessionStorage.getItem(key)}`);
}
saveToBothStorages('sharedKey', 'Shared Value');
//Task 10: Write a function that clears all data from both `localStorage` and `sessionStorage`. Verify that both storages are empty.
// Task 10
function clearAllStorages() {
localStorage.clear();
sessionStorage.clear();
console.log('LocalStorage after clear:', localStorage.length); // Should be 0
console.log('SessionStorage after clear:', sessionStorage.length); // Should be 0
}
clearAllStorages();