Javascript

browser sessionStorage share between tabs

19 September 2026 · 11 min read

browser sessionStorage share between tabs

Have you ever filled out a lengthy form online, only to lose all your progress when you accidentally refreshed the page or navigated away? Or perhaps you wanted to keep some data isolated within a single browser session without persisting it permanently? That’s where browser sessionStorage comes to the rescue. This powerful web API offers a way to store data that is only available for the duration of a user’s browser session. Unlike cookies or localStorage, browser sessionStorage data is cleared when the browser tab or window is closed, providing a convenient and secure way to manage temporary information. Understanding how to effectively use browser sessionStorage can significantly improve the user experience of your web applications, preventing data loss and ensuring a smoother, more intuitive browsing experience. This article will dive deep into how browser sessionStorage works, its limitations, and how it compares to other storage options, answering common questions like “browser sessionStorage share between tabs?”

Understanding Browser SessionStorage

Browser sessionStorage is a web storage object used to store data for a single session. The data is stored in the browser and is only available to the script that created it during that session. This means that when the user closes the browser window or tab, or navigates to a different domain, the data stored in browser sessionStorage is automatically cleared. This makes it ideal for storing temporary data, such as form data, shopping cart information, or authentication tokens that should not persist beyond the current session. Browser sessionStorage is part of the Web Storage API, providing a simple key-value pair mechanism for data storage.

One key characteristic of browser sessionStorage is its scope. Data stored in sessionStorage is specific to the browser tab or window in which it was created. This answers the question: “browser sessionStorage share between tabs?”. The answer is no. If you open the same website in multiple tabs, each tab will have its own separate sessionStorage. This isolation is beneficial for maintaining the integrity of data across different sessions and preventing accidental data overwrites. For instance, if a user is filling out a form in one tab and opens the same form in another tab, the data entered in one tab will not affect the other. This contrasts with localStorage, which is shared across all tabs and windows for the same domain.

Browser sessionStorage offers several advantages over cookies, including larger storage capacity (typically 5MB per origin), a simpler API, and automatic deletion upon session end. According to a study by Mozilla, “Web Storage provides significantly more storage capacity than cookies, allowing for more complex data to be stored client-side” [^1^]. This makes sessionStorage a preferred choice for storing substantial amounts of temporary data. Furthermore, sessionStorage is not sent to the server with every HTTP request, reducing network overhead and improving performance. This is especially important for applications that handle sensitive data or require fast response times. Using browser sessionStorage can greatly enhance the efficiency and security of your web applications.

How to Use SessionStorage in JavaScript

Using browser sessionStorage in JavaScript is straightforward. The Web Storage API provides methods for setting, getting, and removing data. To store data, you can use the setItem() method, passing the key and value as arguments. To retrieve data, you use the getItem() method, passing the key as an argument. To remove data, you can use the removeItem() method, again passing the key as an argument. Finally, to clear all data stored in sessionStorage, you can use the clear() method.

Here’s a basic example of how to use sessionStorage in JavaScript:

// Store data sessionStorage.setItem('username', 'JohnDoe'); // Retrieve data let username = sessionStorage.getItem('username'); console.log(username); // Output: JohnDoe // Remove data sessionStorage.removeItem('username'); // Clear all data sessionStorage.clear(); 

When working with sessionStorage, it’s important to remember that data is stored as strings. If you need to store more complex data types, such as objects or arrays, you’ll need to serialize the data using JSON.stringify() before storing it and then parse it back using JSON.parse() when retrieving it. For example:

// Store an object let user = { name: 'JaneDoe', age: 30 }; sessionStorage.setItem('user', JSON.stringify(user)); // Retrieve and parse the object let storedUser = JSON.parse(sessionStorage.getItem('user')); console.log(storedUser.name); // Output: JaneDoe 

Error handling is also crucial when working with sessionStorage. In some cases, users may have sessionStorage disabled in their browser settings, or the storage quota may be exceeded. You should always wrap your sessionStorage operations in try-catch blocks to handle potential errors gracefully. According to MDN Web Docs [^2^], “It is always a good practice to check if the browser supports Web Storage before attempting to use it.” This ensures that your application can gracefully degrade if sessionStorage is not available.

SessionStorage vs. Other Storage Options

When deciding which storage option to use for your web application, it’s essential to understand the differences between sessionStorage, localStorage, and cookies. Each option has its own strengths and weaknesses, and the best choice depends on the specific requirements of your application. While browser sessionStorage is designed for temporary, session-specific data, localStorage provides persistent storage that remains available even after the browser is closed and reopened. Cookies, on the other hand, are small text files that are stored on the user’s computer and sent to the server with every HTTP request.

Here’s a quick comparison:

  • sessionStorage: Stores data for a single session; data is cleared when the browser tab or window is closed. Not shared between tabs.
  • localStorage: Stores data persistently; data remains available until explicitly deleted. Shared between all tabs and windows from the same origin.
  • Cookies: Stores small amounts of data; data can be configured to expire after a certain time. Sent to the server with every HTTP request.

The choice between these options depends on the type of data you need to store and how long you need to store it. Use browser sessionStorage for temporary data that should not persist beyond the current session, such as form data or authentication tokens. Use localStorage for data that needs to be stored persistently, such as user preferences or offline data. Use cookies for data that needs to be accessed by both the client and the server, such as session identifiers or tracking information. However, be mindful of cookie size limitations and the potential impact on network performance.

Security is another important consideration. Cookies can be vulnerable to cross-site scripting (XSS) attacks if not properly secured. sessionStorage and localStorage are generally considered more secure because they are not automatically sent to the server with every request. However, it’s still important to protect against XSS attacks by sanitizing user input and encoding data properly. Always store sensitive data securely, regardless of the storage option you choose. You can find more information on web storage security best practices from OWASP [^3^].

Best Practices and Common Use Cases

To effectively leverage browser sessionStorage, consider these best practices. First, always handle errors gracefully. SessionStorage might be disabled, full, or otherwise unavailable. Wrap your code in try-catch blocks to prevent unexpected failures and provide informative messages to the user. Second, be mindful of the storage quota. Although 5MB is generally available, exceeding this limit will cause an error. Regularly review and optimize your data storage to avoid exceeding the quota. Third, use descriptive keys. Choose keys that clearly indicate the purpose of the stored data, making your code more readable and maintainable.

Common use cases for browser sessionStorage include:

  • Form Data: Storing form data temporarily to prevent data loss if the user accidentally refreshes the page.
  • Shopping Cart Information: Storing items added to a shopping cart during a single session.
  • Authentication Tokens: Storing authentication tokens to keep the user logged in during the session.
  • Wizard Progress: Storing the user’s progress in a multi-step wizard or form.

Here’s an example of using sessionStorage to store form data:

  1. User starts filling out a form.
  2. As the user enters data, the data is stored in sessionStorage using setItem().
  3. If the user accidentally refreshes the page, the data is retrieved from sessionStorage using getItem() and repopulated in the form fields.
  4. When the user submits the form successfully, the data is cleared from sessionStorage using removeItem() or clear().

This approach significantly improves the user experience by preventing data loss and ensuring a smoother workflow. In addition, consider using sessionStorage to store temporary UI state information, such as the expanded or collapsed state of a navigation menu. This can enhance the user’s browsing experience by preserving their preferences during the session. Remember to always prioritize security and data privacy when working with sessionStorage, especially when storing sensitive information.

Here is a paragraph optimized for a featured snippet:

Browser sessionStorage is a web storage API that allows you to store data for a single session. This data is specific to the browser tab or window and is automatically cleared when the user closes the tab or window. Unlike localStorage, which persists data even after the browser is closed, sessionStorage is ideal for storing temporary information, such as form data or authentication tokens, that should not be available beyond the current session. The key difference is its session-based scope, ensuring data is isolated and cleared automatically.

Infographic here
Frequently Asked Questions (FAQ) --------------------------------
What is the storage capacity of **browser sessionStorage**?
The storage capacity is typically around 5MB per origin, but it can vary slightly depending on the browser.
Is **sessionStorage** data secure?
**SessionStorage** is generally considered more secure than cookies because it is not automatically sent to the server with every HTTP request. However, it is still important to protect against XSS attacks by sanitizing user input and encoding data properly.
Can I use **sessionStorage** across different domains?
No, **sessionStorage** is scoped to the origin (domain, protocol, and port). Data stored in **sessionStorage** for one domain cannot be accessed by another domain.
What happens if the user has **sessionStorage** disabled in their browser?
If **sessionStorage** is disabled, the sessionStorage object will still be available, but attempting to use its methods will likely result in errors. You should always check if the browser supports Web Storage before attempting to use it and handle errors gracefully.
Understanding **browser sessionStorage** empowers you to build more robust and user-friendly web applications. By leveraging its unique session-based storage capabilities, you can enhance the user experience, prevent data loss, and improve application performance. You now know that **browser sessionStorage** does not share data between tabs, providing a secure and isolated storage mechanism for each session. Explore further the capabilities of the Web Storage API, and consider how you can integrate these techniques into your projects. Take a look at our other articles on web development best practices, or [explore more about web performance optimization](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c). The possibilities are endless, and the benefits are significant. Start implementing these techniques today and see the positive impact on your web applications.

[^1^]: Mozilla Developer Network. “Web Storage API.” [https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API) [^2^]: MDN Web Docs. “Using the Web Storage API.” [https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API) [^3^]: OWASP. “Web Storage.” [https://owasp.org/www-project-top-ten/](https://owasp.org/www-project-top-ten/) Question & Answer :
I have some values in my site which I want to clear when the browser is closed. I chose sessionStorage to store those values. When tab is closed they are indeed cleared, and kept if the user presses f5; But if the user opens some link in a different tab these values are unavailable.

How I can share sessionStorage values between all browser tabs with my application?

The use case: put a value in some storage, keep that value accessible in all browser tabs and clear it if all tabs are closed.

if (!sessionStorage.getItem(key)) { sessionStorage.setItem(key, defaultValue) } 

You can use localStorage and its “storage” eventListener to transfer sessionStorage data from one tab to another.

This code would need to exist on ALL tabs. It should execute before your other scripts.

// transfers sessionStorage from one tab to another var sessionStorage_transfer = function(event) { if(!event) { event = window.event; } // ie suq if(!event.newValue) return; // do nothing if no value to work with if (event.key == 'getSessionStorage') { // another tab asked for the sessionStorage -> send it localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage)); // the other tab should now have it, so we're done with it. localStorage.removeItem('sessionStorage'); // <- could do short timeout as well. } else if (event.key == 'sessionStorage' && !sessionStorage.length) { // another tab sent data <- get it var data = JSON.parse(event.newValue); for (var key in data) { sessionStorage.setItem(key, data[key]); } } }; // listen for changes to localStorage if(window.addEventListener) { window.addEventListener("storage", sessionStorage_transfer, false); } else { window.attachEvent("onstorage", sessionStorage_transfer); }; // Ask other tabs for session storage (this is ONLY to trigger event) if (!sessionStorage.length) { localStorage.setItem('getSessionStorage', 'foobar'); localStorage.removeItem('getSessionStorage', 'foobar'); }; 

I tested this in chrome, ff, safari, ie 11, ie 10, ie9

This method “should work in IE8” but i could not test it as my IE was crashing every time i opened a tab…. any tab… on any website. (good ol IE) PS: you’ll obviously need to include a JSON shim if you want IE8 support as well. :)

Credit goes to this full article: http://blog.guya.net/2015/06/12/sharing-sessionstorage-between-tabs-for-secure-multi-tab-authentication/