I’m trying to store some data through localStorage
so I can load it again in the same page of a web if this page is refreshed.
Imagine a user writing a comment, he refreshes the page and I want to load again the comment through localStorage
so the user can keep editing his comment after that refresh.
The issue I’m facing is that, what if the user has multiple tabs open on the same page, writing different comments? How could I retrieve the correct information for one specific tab?
2
Answers
Just make a storage key unique. For example include a topic or replied comment id in it:
You can use also
sessionStorage
that unique per a tab. But closing the tab will destroy it.SessionStorage is exactly what you need. It behaves the same way as
localStorage
, but is dedicated to the page session. The downside ofsessionStorage
is that if the user will close the tab, he’ll loose it.So, depending on your use case you can use:
See: