-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalStorage.html
More file actions
122 lines (108 loc) · 5.3 KB
/
Copy pathlocalStorage.html
File metadata and controls
122 lines (108 loc) · 5.3 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
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
<!DOCTYPE html>
<html lang="en" class="h-100">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous" />
<link href="./prism.css" rel="stylesheet" />
<title>Local Storage Test</title>
</head>
<body class="d-flex flex-column h-100">
<nav class="navbar navbar-expand-sm navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="./index.html">JS Playground</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
Sandboxes
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item active" href="./localStorage.html">Local Storage</a></li>
<li><a class="dropdown-item" href="./openWeatherAPI.html">APIs</a></li>
<li><a class="dropdown-item" href="./stylesTest.html">Styling</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
<h3>Local Storage </h3>
<h5>Example:</h5>
<p>
<button class="btn btn-primary" onclick="clickCounter()" type="button">
Click me!
</button>
</p>
<div id="counterResult"></div>
<p>Click the button to see the counter increase.</p>
<p>
Close the browser tab (or window), and try again, and the counter will
continue to count (it is not reset).
</p>
<p>
<button class="btn btn-primary" onclick="seeStorage()" type="button">
What's in storage?
</button>
</p>
<p>
When inserting into local storage, we should always stringify data before saving it!
</p>
<pre class="rounded-3"><code id="badStorageResult" class="language-plain" ></code></pre>
<pre class="rounded-3"><code id="goodStorageResult" class="language-plain" ></code></pre>
<h3>Code Used: </h3>
<pre class="rounded-3"><code id="codeDisplay" class="language-javascript" ></code></pre>
</div>
<footer class="footer mt-auto py-3">
<div class="container text-center">
<span class="text-muted">By <a href="https://sol3.me">@sol3uk</a>.</span>
</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/components/prism-core.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/plugins/autoloader/prism-autoloader.min.js"></script>
<script id="codeForPage">
var objectTest;
function clickCounter() {
if (typeof (Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("counterResult").innerHTML = `You have clicked the button ${localStorage.clickcount} time(s).`;
} else {
document.getElementById("counterResult").innerHTML = "Sorry, your browser does not support web storage...";
}
}
function seeStorage() {
objectTest = {
hello: "world",
};
//Bad - Not stringified.
localStorage.objectTest = objectTest;
//Good - Stringified before insertion.
localStorage.stringifiedObjectTest = JSON.stringify(objectTest);
document.getElementById("badStorageResult").innerHTML = `Inserted Directly: ${localStorage.objectTest}`;
document.getElementById("goodStorageResult").innerHTML = `Stringified Before: ${localStorage.stringifiedObjectTest}`;
}
</script>
<script>
$('#codeDisplay').html($('#codeForPage').html())
</script>
</body>
</html>