Created
July 28, 2020 16:37
-
-
Save nomyfan/255cbe8cb1bfef67e6ca06728990eace to your computer and use it in GitHub Desktop.
Read session storage value from another tab that is the opener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getSessionItem(key, remove) { | |
if (!key) { | |
throw new Error(`invalid key: ${key}`); | |
} | |
if (remove === undefined) { | |
remove = true; | |
} | |
let o = window.sessionStorage.getItem(key); | |
if (!o && window.opener) { | |
o = window.opener.sessionStorage.getItem(key); | |
} | |
if (window.opener && remove) { | |
window.opener.sessionStorage.removeItem(key); | |
} | |
return o || undefined; | |
} | |
function setSessionItem(key, value) { | |
if (!key) { | |
throw new Error(`invalid key: ${key}`); | |
} | |
window.sessionStorage.setItem(key, value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment