2016-03-02 53 views

回答

0

有,但它不能保證永遠是正確的:

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的。但請注意,如果瀏覽器崩潰,該值仍然保存。

+0

我知道了,謝謝,我試圖讓檢查網頁上的用戶活動。我爲此做了邏輯,唯一的問題是,如果用戶打開多個選項卡,如果他在第一次打開時不活躍,他會從所有其他選項卡中刪除......而我不想那樣做。 –

4

您可以使用我的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'); 

JSFIDDLE

相關問題