5

Is there a way to detect if multiple browser tabs are opened of the same application.

Let's say i have www.test.com and i open 4 tabs of this website.

Is there a way to detect that multiple tabs are opened in JavaScript?

Vesko Vujovic
  • 362
  • 1
  • 5
  • 23
  • Does this answer your question? [Stop people having my website loaded on multiple tabs](https://stackoverflow.com/questions/11008177/stop-people-having-my-website-loaded-on-multiple-tabs) – Pere Joan Martorell Feb 02 '22 at 15:37

2 Answers2

4

You can use my sysend.js library, it use localStorage to send messages between open tabs/windows. All to do is this code:

sysend.on('notification', function() {
   sysend.broadcast('multiple');
});
sysend.on('multiple', function() {
   // this will fire n-1 times if there are n tabs open if n > 1
   alert('multiple pages');
});
sysend.broadcast('notification');

JSFIDDLE

jcubic
  • 61,973
  • 54
  • 229
  • 402
2

There is, but it's not guaranteed to always be correct:

window.onload = function() {
  var applicationCount = localStorage.getItem("applicationCount");
  if (!applicationCount) {
    applicationCount = 0;
  }
  localStorage.setItem("applicationCount", ++applicationCount);
}

window.onbeforeunload = function(e) {
  var applicationCount = localStorage.getItem("applicationCount");
  if (!applicationCount) {
    applicationCount = 1;
  }
  localStorage.setItem("applicationCount", --applicationCount);
};

It uses the localStorage which is shared among the tabs. But note that if the browser crashes, the value is still saved.

Andy
  • 141
  • 1
  • 7
  • I know, thanks, i'm trying to make check for user inactivity on the page. I made logic for that, only problem is if user opens multiple tabs if he's not active in the first opened he will we loged out from all others tabs... And i don't want that. – Vesko Vujovic Mar 02 '16 at 15:35