Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Created December 21, 2020 01:31
Show Gist options
  • Save sandipchitale/09d8167f04f6a0be28d64372323f0803 to your computer and use it in GitHub Desktop.
Save sandipchitale/09d8167f04f6a0be28d64372323f0803 to your computer and use it in GitHub Desktop.
Assign next tabId to every new tab and save in session storage
(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