Objective: To solve the problem that sessionStorage does not have sessionStorage data in the new TAB page when you click the browser right click ‘open in new TAB page’ or copy the URL to paste in a new TAB page and turn to it. If yes, the user logs in again.
Assume that page A contains A sessionStorage and page B is A page whose copy address is opened on A new TAB page. In this case, page B does not have the sessionStorage (sessionStorage feature) of the original page. You can use the following method to achieve sharing.
The following example is A sessionStorage where I want to get auth-code and secretKey keys from page A on page B.
(function() {
const AuthCode = () = > sessionStorage.getItem('Auth-Code');
const secretKey = () = > sessionStorage.getItem('secretKey');
// If page B does not have these two sessionStorages
if(! AuthCode() || ! secretKey()) {GetSessionStorage = getSessionStorage = getSessionStorage = getSessionStorage
localStorage.setItem('getSessionStorage'.Date.now());
};
// Target event
window.addEventListener('storage'.function(event) {
// Page A has obtained the getSessionStorage event triggered by page B
if (event.key == 'getSessionStorage') {
LocalStorage = localStorage; // localStorage = localStorage
// Immediately delete the sessionStorage
localStorage.setItem('sessionStorage'.JSON.stringify({
'Auth-Code': AuthCode() ?? ' '.'secretKey': secretKey() ?? ' '
}));
localStorage.removeItem('sessionStorage');
// Now page B has obtained the sessionStorage event triggered by page A (before deletion) and page B does not have these two sessionstorages
} else if (event.key == 'sessionStorage'&&! AuthCode() || ! secretKey()) {// sessionStorage = sessionStorage = sessionStorage = sessionStorage
var data = JSON.parse(event.newValue);
for (key indata) { sessionStorage.setItem(key, data[key]); }}}); } ());Copy the code