2

I want my browser tab to know when it has been duplicated.

The only similar solutions I've seen focus on just the duplicated tab knowing it's been duplicated or the original tab knowing it's been duplicated but this only works on Chrome.

Requirements:

  • Solution works on all modern browsers

Bonus if the solution:

  • Works on a version of Internet Explorer
  • Uses some native Angular functionality
Ian Steffy
  • 1,234
  • 3
  • 18
  • 45

1 Answers1

5

You can make use of Broadcast Channel API .

  1. Create a channel on load and broadcast the message
  2. use the onmessage event, capture the message and if it matches the initial one, broadcast another
  3. The duplicate tag will receive the second broadcast , while first tab receives the first broadcast

 const broadcast = new BroadcastChannel('test')
    broadcast.postMessage('I am First');
    broadcast.onmessage = (event) => {
      if (event.data === "I am First") {
        broadcast.postMessage(`Sorry! Already open`);
        alert("First Tab");
      }
      if (event.data === `Sorry! Already open`) {
        alert("Duplicate Tab");
      }
    };
Ranjith Jayaram
  • 300
  • 2
  • 13
  • This solution is perfect and vastly superior to all other solutions ive seen. Before, I had individual functions that check if the tab is a duplicate or if the tab is duplicated, and this was set on intervals. Your function, however does both and automatically triggers when the original tab is duplicated. Hats off to you @Ranjith – Ian Steffy Jan 04 '22 at 11:42