Reputation: 81
I have a webpage (home.html) and I want it be accessible only once at a time.
That is, a person can enter that page the first time he/she opened. But while this page is viewing in a tab, another page will be opened if the person try to open home.html in a new tab.
How can I do that? Which JS function I need to add in my webpage?
Upvotes: 2
Views: 1571
Reputation: 8457
local storage with trigger on body load/unload events:
function session1() {
var result = localStorage.getItem("session");
if (result != "running") {
localStorage.setItem("session", "running");
} else if (result == "running") {
localStorage.setItem("redirect", "yes");
window.location.replace("http://www.google.com");
}
}
function session0() {
var stats = localStorage.getItem("redirect");
if (stats == "yes") {
} else {
localStorage.setItem("session", "done");
}
localStorage.setItem("redirect", "no");
}
<body onload="session1()" onunload="session0()">
<div style="background:skyblue">SESSION RUNNING</div>
Upvotes: 2
Reputation: 1007
You could try to fiddle with localStorage and onbeforeunload event, something along the lines of:
if(localStorage['visited'] == true){
document.getElementsByTagName('body')[0].innerHTML = 'This page is opened twice.';
}
localStorage['visited'] = true;
window.onbeforeunload = function(){
localStorage['visited'] = false;
}
Upvotes: 0