有沒有辦法檢測是否有多個瀏覽器選項卡打開了同一個應用程序。檢測在javascript中打開同一應用的多個Chrome選項卡
比方說,我有www.test.com,我打開本網站的4個選項卡。
有沒有辦法檢測到在JavaScript中打開多個選項卡?
有沒有辦法檢測是否有多個瀏覽器選項卡打開了同一個應用程序。檢測在javascript中打開同一應用的多個Chrome選項卡
比方說,我有www.test.com,我打開本網站的4個選項卡。
有沒有辦法檢測到在JavaScript中打開多個選項卡?
有,但它不能保證永遠是正確的:
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);
};
它使用該選項卡之間共享localStorage的。但請注意,如果瀏覽器崩潰,該值仍然保存。
您可以使用我的sysend.js library,它使用localStorage在打開的標籤頁/窗口之間發送消息。所有要做的就是這段代碼:
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');
我知道了,謝謝,我試圖讓檢查網頁上的用戶活動。我爲此做了邏輯,唯一的問題是,如果用戶打開多個選項卡,如果他在第一次打開時不活躍,他會從所有其他選項卡中刪除......而我不想那樣做。 –