Can sessionStorage between multiple tabs be directly shared and accessed?

First, let’s understand what sessionStorage is on the next page.

1. The current sessionStorage value of page A is 123. I refresh the page with F5. Is the sessionStorage value still there?

Answer: Yes.

2. The current sessionStorage value of page A is 123. I copy the address of page A and open a new TAB page in the browser and paste the address to access page A. Is the sessionStorage value of 123 still there?

Answer: No.

Opening multiple Tabs pages with the same URL will create their own  sessionStorage.

 3. The current sessionStorage value of page A is 123. I close the tab of page A and use the browser’s opening history page to restore page A. Is the sessionStorage value still there?

Answer: Yes.

The page session is maintained while the browser is open, and reloading or restoring the page will maintain the original page session.

4. Can sessionStorage between multiple tabs be directly shared and accessed?

Okay, back to the question itself, "Can sessionStorage between multiple tabs be directly shared and accessed?" Don't just say no at once. The answer to this question will take a few minutes. 

Situation 1: Can the sessionStorage of pages with different domain names be directly shared and accessed?

Answer: No.

 Scenario 2: Can sessionStorage of pages with the same domain name be directly shared and accessed?

There are many situations here:

The following A and B are both under the same domain name. Take the sessionStorage value of "window.sessionStorage.setItem("This is the first page", "123")" as an example.

1. In page A, click the button to call window.open('./B.html) to open page B. We can see that the sessionStorage in page B is the same as the sessionStorage in page A.

Opening via a tag is the same as window.open

<a href="./B.html" target="_block">这是跳转B页面的a标签</a>

Answer: Page B can access the sessionStorage value of page A.

 

The above situation raises a new question. Does page B directly share the session storage of page A? Or copy a copy of the sessionStorage of page A and put it on page B?

Exploration process: We execute window.sessionStorage.setItem("This is the first page", "456") on page A and modify the value of sessionStorage. We can see that the sessionStorage of page A has changed, and in When the session storage value of page A is still 123, the sessions storage value of page B opened through window.open is still 123.

2. There is a sessionStorage value in page A. We directly copy the address of page B and create a new tab page in the same window of the browser. Enter the address of page B and press Enter to open page B.

Answer: Page B cannot copy the sessionStorage value accessed to page A.

Based on the above situation, we draw a conclusion:

SessionStorage of different domain names cannot be shared, and sessionStorage status cannot be shared between multiple windows of the same domain name! ! !

But in some specific scenarios: the same-origin page opened as a new tab or window in this page will copy the sessionStorage of the previous page. But it is just a copy. If the sessionStorage is modified on the A page, the sessionStorage value of the opened B page will be updated accordingly, unless you open a new B page on the A page.

Guess you like

Origin blog.csdn.net/tt18473481961/article/details/131569918