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
2 Answers
Reset to default 4You 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.