# Local storage
- Local storage allows developers to store and retrieve data in the browser
- The stored data will not expire and will persist even if the user closed the browser window
- Local storage is part of the Web storage API (opens new window)
- Local storage stores a key/value combination inside the client's browser
- The value can be any type of data (a string, a number, an array, a JSON object, ...), as long as you can convert it to a string
methode | description |
---|---|
localStorage.setItem('key', 'value') | save the key with his value to local storage |
localStorage.getItem('key') | retrieve the value from a specific key from local storage |
localStorage.removeItem('key') | remove the key from local storage |
localStorage.clear() | remove all keys from local storage |
- Because the value can only be a string, you will mostly have to convert your variables before storing them:
localStorage.setItem('key', JSON.stringify(<variable>))
let variable = JSON.parse(localStorage.getItem('key'))
# Examples
# Store form data
- Saving form data is one of the most used features of local storage
- In this example we the username and password on a login form, so it's already filled in the next time you open te page
- Open es6/storage/login.html and es6/storage/login.js
HTML
JavaScript
result
<form action="https://mcloots.sinners.be/form.php" method="post" id="loginform"> ... <input type="text" id="username" name="username" placeholder="Username" /> ... <input type="password" id="password" name="password" placeholder="Password" /> ... <button type="submit" class="tertiary">Submit</button> </form>
Copied!
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Shoppinglist
- In this example, we introduce a new HTML property
contenteditable
(opens new window) e.g:<div contenteditable>milk</div>
- This attribute allows us to edit the content of the element without the need for an additional input field
- Open es6/storage/shopping.html and es6/storage/shopping.js
HTML and CSS
JavaScript
result
<!DOCTYPE html> <html lang="en"> <head> ... <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" /> <style> .fas { padding: 0.25rem 0.5rem; color: #aaa; cursor: pointer; } .border-gray > div { display: flex; align-items: center; padding: 1rem 0; color: darkgreen; } .border-gray > div:not(:last-child) { border-bottom: solid 1px #ddd; } [contenteditable] { width: 100%; cursor: text; padding: 0.25rem 1rem; } [contenteditable]:focus { outline: 1px solid #ddd; } .strike { text-decoration: line-through; color: darkred; } </style> </head> <body> ... <div class="border-gray"></div> ... <script src="shopping.js"></script> </body> </html>
Copied!
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
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
# Store accordion state (advanced)
- Every main section inside this course has some kind of 'accordeon' tabs that contains some links to examples or exercises
- The state of every tab (open or closed) is saved in local storage
- Because the same logic is used in three different pages, we decided to work with a class
- Open es6/index.html and js/classes/StoreTabs.js
index.html
StoreTabs.js
result
- One accordion is made up of three parts
- Line 6: a
div
that contains the links to the examples/exercises - Line 5: a
label
containing the the text of the accordion - Line 4: a
checkbox
to open (checkbox
is checked) or close (checkbox
is not checked) the associateddiv
tag
- Line 6: a
- Line 9: link to the
StoreTabs
class - Line 11: instantiate a new
StoreTabs
object with the namees6
This name will be used as the key for our local storage
<body> ... <div class="collapse"> <input type="checkbox" id="syntax" name="accordion" /> <label for="syntax" id="syntaxLabel">Basic syntax</label> <div>...</div> </div> ... <script src="../js/classes/StoreTabs.js"></script> <script> const openTabs = new StoreTabs('es6'); </script> </body>
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# References
- Local storage@Mozilla (opens new window)
- Local storage@W3Schools (opens new window)