Created
December 21, 2020 01:31
-
-
Save sandipchitale/09d8167f04f6a0be28d64372323f0803 to your computer and use it in GitHub Desktop.
Assign next tabId to every new tab and save in session storage
This file contains hidden or 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() { | |
// Does this tab have a tabId | |
let tabId = sessionStorage.getItem("tabId"); | |
if (tabId === null) { | |
// no tabId for this tab | |
// Is lastTabId stored in local storage | |
let lastTabId = localStorage.getItem("lastTabId"); | |
if (lastTabId === null) { | |
// no lastTabId saved in local storage | |
lastTabId = -1; | |
} | |
// Generate next tabId | |
lastTabId = 1 + parseInt(lastTabId); | |
// Save as lastTabId | |
localStorage.setItem("lastTabId", lastTabId); | |
// assign | |
tabId = lastTabId; | |
// store tabId | |
sessionStorage.setItem("tabId", tabId); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment