最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - Detect multiple chrome tabs opened of the same app in javascript - Stack Overflow

programmeradmin0浏览0评论

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

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

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

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

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

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

Share Improve this question asked Mar 2, 2016 at 15:13 Vesko VujovicVesko Vujovic 4001 gold badge7 silver badges24 bronze badges 1
  • Does this answer your question? Stop people having my website loaded on multiple tabs – Pere Joan Martorell Commented Feb 2, 2022 at 15:37
Add a ment  | 

2 Answers 2

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论