Chapter 2. Simple Persistence with the Web Storage API
Introduction
The Web Storage API persists simple data locally, in the user’s browser. You can retrieve this data later, even after closing and reopening the browser.
This API has a Storage interface that provides data access and persistence. You don’t create instances of Storage directly; there are two global instances: window.localStorage and window.sessionStorage. The only difference between these is how long they retain the data.
sessionStorage data is associated with a specific browser session. It retains the data if the page is reloaded, but closing the browser completely loses the data. Different tabs for the same origin do not share the same persisted data.
On the other hand, localStorage shares the same storage space across all tabs and sessions for the same origin. The browser retains this data even after you close the browser. In general, session storage is a good choice if you want to store something ephemeral or sensitive that you want to be destroyed once the browser is closed.
In both cases, storage space is specific to a given origin.
Getting and Setting Items
Web Storage can only store string values. Each value has a key that you can use to look ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access