Can sessionStorage Share Data Across Tabs? Experiments and Conclusions
This article explains the differences between localStorage and sessionStorage, demonstrates through code experiments that sessionStorage does not share data across multiple tabs unless the new page is opened via window.open or a link, and summarizes the resulting behavior.
When discussing web storage APIs, developers often compare localStorage and sessionStorage . localStorage provides persistent, origin‑wide storage that survives browser restarts, while sessionStorage stores data only for the duration of a page session and is cleared when the tab or window is closed.
Both APIs are read‑only from the perspective of the Storage object, but their lifetimes differ. Data saved in localStorage remains available across all tabs of the same origin, whereas data in sessionStorage is isolated to the specific tab that created it.
// Store a value in the first tab
localStorage.setItem('name', 'fatfish');
// Retrieve it in another tab
localStorage.getItem('name'); // "fatfish"According to MDN, a new tab opened from an existing page copies the top‑level browsing session’s context, which suggests that sessionStorage might be shared. To verify this, several tests were performed on the Juejin homepage.
// Set a flag in sessionStorage
window.sessionStorage.setItem('canShare', 'yes');Opening a random article in a new tab and reading the flag returned null , indicating that the data was not shared. However, when the new page was opened using window.open (or by clicking a link without target="_blank" ), the flag could be read successfully:
// Open a new page via window.open
window.sessionStorage.setItem('canShare','yes');
window.open('https://juejin.cn/post/7342793254096109583', 'aa');
// In the new page
window.sessionStorage.getItem('canShare'); // "yes"These observations lead to the conclusion that sessionStorage does not share data across independent tabs or windows , but when a new page is opened from an existing one using window.open or a normal link (without forcing a new window), the new page inherits the originating page’s sessionStorage contents.
Therefore, developers should not rely on sessionStorage for cross‑tab communication; instead, localStorage or other messaging mechanisms should be used.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.