What causes the session storage to be accessed across various browser sessions?

Scenario
While working on an application, I discovered an intriguing behavior in Chrome 62 on Windows 10 related to defining values in sessionStorage. Surprisingly, changing a value in one tab affected other tabs that shared the same key.

Initially, I believed that localStorage was intended to persist information across all browser windows, whereas sessionStorage was meant for specific window or tab persistence.

Specifically, I have an AngularJS service dedicated to handling interactions with sessionStorage:

export class PersistenceSvc {
    public static $inject: string[] = ['$window'];
    public constructor(public $window: ng.IWindowService) {}

    public save<T>(name: string, data: T): void {
        const saveData: string = JSON.stringify(data);
        this.$window.sessionStorage.setItem(name, saveData);
    }

    public load<T>(name: string): T {
        const loadData: string = this.$window.sessionStorage.getItem(name);
        const result: T = JSON.parse(loadData) as T;
        return result;
    }
}

...which is utilized from a run block to facilitate data persistence within the application.

export function persistSomeData(
    someSvc: Services.SomeService, 
    userAgentSvc: Services.UserAgentSvc,
    persistenceSvc: Services.PersistenceSvc,
    $window: ng.IWindowService) {
    if(userAgentSvc.isMobileSafari()) {
        // Special instructions for iOS devices.
        return;
    }

    const dataToPersist: Models.DataModel = persistenceSvc.load<Models.DataModel>('SomeData');
    if(dataToPersist) {
        // Update someSvc state with loaded data.
    } else {
        // Fetch necessary data from the server.
    }

    $window.onbeforeunload = () => {
        persistenceSvc.save<Models.DataModel>('SomeData', someSvc.dataState);
    };
}

persistSomeData.$inject = [
    // All necessary module names, not shown in this example.
];
angular
    .module('app')
    .run(persistSomeData);

When using a single tab, everything functions correctly (excluding iOS device issues). However, when following these steps, unexpected behavior arises...

Steps:
1. Open Chrome and create a new tab which is dragged out into its own window.
2. Visit your site utilizing the code mentioned above.
3. Perform actions causing data state changes in the first browser instance.
4. Do the same in the second browser instance.
5. Interact with the site in a manner that accesses the data state of the first browser.
6. Notice that the data retrieved in the first browser actually came from the second browser instance. (This is the issue at hand.)

Inquiry:
Given my limited experience with cookie/localStorage/sessionStorage coding, it's entirely possible that I've misunderstood something. With that said, why does window.sessionStorage exhibit behavior contrary to what's suggested by both MDN documentation and the top answer on this SO question?

EDIT: There seems to be an issue, but it's unrelated to client-side problems. Closing this inquiry as my assumption about the client being responsible was mistaken.

Answer №1

Your code seems to have an issue based on a simple test in the browser console, which indicates that sessionStorage only affects the active browser tab. Changes made in one tab do not reflect in another tab:

https://i.stack.imgur.com/XinB6.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Node.js and Jasmine: Preventing the invocation of a Promise function to avoid executing the actual code results in DEFAULT_TIMEOUT_INTERVAL

There is a function below that returns a promise. public async getAverageHeadCount(queryParams: Params, childNode: any, careerTrackId: string): Promise<Metric> { const queryId = this.hierarchyServiceApiUrl + "rolling-forecast/ahc/" + q ...

How can I update a dropdown menu depending on the selection made in another dropdown using Angular

I am trying to dynamically change the options in one dropdown based on the selection made in another dropdown. ts.file Countries: Array<any> = [ { name: '1st of the month', states: [ {name: '16th of the month&apos ...

When using AngularJS 2, the class identity is lost when resolving a Promise during fetching

SUMMARY: I'm encountering an issue where I am fetching Object instances instead of Org instances from my data in Angular 2. Is there a way to retrieve Org objects directly or is this the expected behavior? DETAILS: In my Angular 2 project, I have mod ...

Tips for applying an active class to buttons using ng-click?

Here is the HTML code for the buttons. The taskfilter is the filter that controls how the buttons work when clicked and the class name is 'sel' <a class="clear-completed" ng-click="taskfilter = 1" ng-class="{'sel':enabled}"> &l ...

Unit Testing Angular controllers with an external variable in Jasmine framework

Looking for ways to create a unit test for a controller that utilizes a $scope.action variable that is defined outside the controller. controller( "MyController", [ "$scope", "$window", "httpInterceptor", function($scope, Service1, Service2, $wind ...

Utilize AngularJS to create a custom tag with a directive included

I've been working on creating a custom tag for a reusable component in a web page, but I seem to have hit a roadblock. It's not functioning as expected, and I feel like I might be overlooking something. If anyone could provide some guidance or p ...

Connect ngModel to an AngularJS directive using a dedicated scope

In an attempt to develop a unique Angular directive that displays radio inputs along with their corresponding labels, the HTML structure for this directive is as follows: <d-radio name="gender" value="male" label="I'm a male"></d-radio> & ...

What is the best way to manage Page Refresh using Angular.js?

I recently followed the tutorial at http://scotch.io/bar-talk/setting-up-a-mean-stack-single-page-application This guide went over controllers and services using angular.js for a single-page application. However, when I try to directly access /pageName o ...

I am looking to convert the input format of a timepicker using moment.js to display as 12:38:07 instead of 2018-01-23T12:38:07.439Z

In my project, I am utilizing AngularJS Material for the template and AngularJS for JavaScript. Due to the absence of a timepicker in Angular Material JS, I have opted to use a timepicker provided by Moment.js. The backend of my application is built using ...

Just starting out with React and encountering the error: Invalid element type, a string was expected

I seem to be going in circles with the following issue as I try to load the basics of a React app into the browser. An error message stating 'Element type is invalid: expected a string (for built-in components) or a class/function (for composite c ...

Incorporating an alternate object array to update an array of objects: A

There are two object arrays, the main array and the temp array. The goal is to compare the main array with the temp array and update the values in the main array based on matching IDs. In this example, IDs 2 and 3 match in both arrays. Therefore, the valu ...

I'm curious about how to link a JSON field using dot notation in Angular 12 HTML

Does anyone know how to bind a JSON field using dot paths in Angular 12 HTML? For example: //Angular data: any = { name: 'x1', address: { city: 'xyz' } }; field: any = 'address.city'; //Html <input [(ngModel)]="data[ ...

"Unlocking the potential of AngularJS: A guide to accessing multiple controllers

I am trying to set a variable in one instance of a controller and read it in another. The variable I need to set is within the object vm (so $scope cannot be used). This is the code for the controller: app.controller("AppController", function(){ var ...

Tips on how to retrieve an Observable Array instead of a subscription?

Is there a way to modify this forkJoin function so that it returns an observable array instead of a subscription? connect(): Observable<any[]> { this.userId = this.authService.userId; this.habits$ = this.habitService.fetchAllById(this.userId); this.s ...

What is the process for creating an index signature for a type alias representing a Map in Typescript?

Suppose I have a custom type for a Map as follows: type MyCustomMap = Map<string, number>; Is there any way to add an index signature to this type so that I can set key-value pairs after initializing it? I have been able to achieve this with types ...

The distinction between http://www.app.in and http://app.in lies in their URL structures. While

Can someone explain the difference between and ? Also, how does this difference impact the cross-domain policy of Ajax? I have added an ajax request in my app: $.ajax({ type: "POST", url: "http://app.in/getToken", contentType: "text/html", ...

Alert: VirtualizedList warns of slow updates for a large list despite optimized components

Struggling with avoiding the warning message "VirtualizedList: You have a large list that is slow to update" while utilizing the <FlatList> component in React-Native. Despite thorough research and attempts at finding a solution, including referencin ...

Angular 6: TypeError - The function you are trying to use is not recognized as a valid function, even though it should be

I'm currently facing a puzzling issue where I'm encountering the ERROR TypeError: "_this.device.addKeysToObj is not a function". Despite having implemented the function, I can't figure out why it's not functioning properly or callable. ...

Building an integrated Monaco editor using Electron and AngularJS

Struggling to integrate the Monaco editor into my Electron app. Despite following electron examples, encountering persistent errors in my application. The errors I'm facing include: "loader.js:1817 Uncaught Error: Unrecognized require call" "angula ...

Combine two closely related functions into a single function

I'm dealing with two very similar functions: setEndTimesAndStartTimes(pricerules: PriceRule[], type: string) { this.endTimes = []; this.startTimes = []; pricerules.forEach(pricerule => { if (type === 'end') { ...