Skip to content

Instantly share code, notes, and snippets.

View maateusilva's full-sized avatar

Mateus Silva maateusilva

View GitHub Profile
@maateusilva
maateusilva / detectabs.js
Last active November 26, 2021 19:38
JavaScript Snippet to detect when tab is closed
// Get or Create a new tab id (accessible only on the tab itself)
var tabID = sessionStorage.tabID ? sessionStorage.tabID : sessionStorage.tabID = Math.random();
// Get all connected tabs from localStorage (shared between all tabs)
var connectedTabs = localStorage.getItem("ConnectedTabs");
// Validate if tabID was created
if (tabID) {
if (!connectedTabs) {
// If there's no tabs connected yet, create an array with itself tabID
var tabs = [tabID];