Issue
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
Solution
You can make use of Broadcast Channel API .
- Create a channel on load and broadcast the message
- use the
onmessage
event, capture the message and if it matches the initial one, broadcast another - 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");
}
};
Answered By - Ranjith Jayaram
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.