-1

In Microsoft Edge, when I right click on a link and use "Open in new tab" option, then I can see/access sessionStorage values created in initial page. However, this issue is not there in Chrome and Firefox browsers. You can see this issue in this JSFiddle.

  1. Open JS Fiddle link (in Edge)
  2. Enter any text. Observe entered text is shown in output section text field item
  3. Right Click on "Run" button and choose "Open in new tab". Observe text entered in previous step in another browser tab is accessible in this new tab. As per documentation sessionStorage is created for each tab and it is private to the tab.

If you repeat these steps in Chrome or Firefox, you wont see this issue. Why does sessionStorage in Edge browser behaves differently from other browsers like Firefox and chrome? Is there any work around for this issue in Edge browser?

<span>Your Name is</span>&nbsp;<input type="text" id="name"/>
(function() {
// check value exists or not in session storage
if (sessionStorage.getItem("username") == null || sessionStorage.getItem("username") == undefined)
{
   vUserName = prompt("Enter your name");
   sessionStorage.setItem("username", vUserName);
}

document.getElementById("name").value = sessionStorage.getItem("username");
})();
TylerH
  • 20,799
  • 66
  • 75
  • 101
Hari_639
  • 101
  • 1
  • 11

2 Answers2

2

The reason is:

sessionStorage.getItem("username") == null // false

Instead, you should use:

sessionStorage.removeItem("username")

Calling getItem now would result in a return value of null, not "null".

TylerH
  • 20,799
  • 66
  • 75
  • 101
linthertoss
  • 166
  • 6
  • looks like its bug in Chrome and Firefox and working as expected in Edge. See below response from Yu Zhou. Thank you. – Hari_639 Jan 02 '20 at 10:13
1

From the doc, it says Opening a page in a new tab or window creates a new session with the value of the top-level browsing context. So I think the page opened from right clicking on a link and using "Open in new tab" option should inherit the sessionStorage from the former page. You could also refer to the answer in this thread, I think it's reasonable.

Besides, there're also people think it's an issue with Chrome. So I think it's a different behavior which is by design in different browsers. You could also refer to this thread for the solution.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
  • Thank you. Looks like it's a bug in Firefox and Chrome and it is working as expected in Edge. I will try window.name techniques specified in another thread. Thank you. – Hari_639 Jan 02 '20 at 10:19