Introduction
If you want to store something in the front-end of a website, which means storing it in the browser, there are basically a few options:
- Cookie
- LocalStorage
- SessionStorage
- IndexedDB
- Web SQL
The last two are rarely used, and the last one, Web SQL, was declared deprecated a few years ago. Therefore, when it comes to storing data, most people mention the first three, with the first two being the most commonly used.
After all, when storing data in the front-end, most data is expected to be stored for a period of time, and cookies and localStorage are designed for this purpose. However, sessionStorage is not, as it is only suitable for storing very short-term data.
I don’t know if your understanding of sessionStorage is the same as mine, so let me explain my understanding:
The biggest difference between sessionStorage and localStorage is that the former only exists in one tab. When you close the tab, the data is cleared, so a new tab will have a new sessionStorage, and different tabs will not share the same sessionStorage. However, if it is the same website, the same localStorage can be shared.
But let me ask you this: Is it possible that in a certain scenario, I store something in sessionStorage in tab A, and then a new tab B can also read the sessionStorage in tab A?
You might think it’s impossible, and I used to think so too, as did my colleagues.
But it turns out that it is possible.
Read More