Hello! I am looking for some help with the following issue:
Imagine a wife has signed on to a web site in a browser tab. She does not sign off and walks away to do something else. Her husband sits at the computer and seeing her signed on, opens a new tab and tries to sign on to the same site.
I need to automatically sign the wife off when this happens so as to avoid confusion later regarding which session is active.
The only way you could really do this is with Ajax and PHP sessions if your using PHP as the backend source. What you would need to do is set a session that stores the IP and some other information about the user logged in and then run that again the user trying to sign in.
If the IP doesn’t match and all the other information then let them continue otherwise stop the script and let the user know a user with the same IP is already logged in.
On the sign on page (or even better, the sign off page), when the sign on button is clicked, a piece of Javascript code is run first. The javascript simply goes through all tabs on the browser, and checks to see if any such tabs has an HTML document that came from the same domain as the sign on page. If such tabs exist, then before POSTing the user name and password to the server, the Javascript code will close those tabs that contain HTML documents came from the same domain.
So, I understand what the code to set the window.name to specific value would look like. But do you know what the code on the sign on/sign off page to close the other tabs/windows would look like?
Thanks again for your help. It is much appreciated.
By default you can’t close tabs in browsers that were not opened by javascript, currently only Firefox from what i have read has the ability to set a dummy window.open method which allows you to close the tab but thats as far as you can get.
To get a list of tabs you would need to build a browser extension for all browsers that gives you control over that and that for me would be a good learning experience but a huge time waster in my opinion.
Hi, SgtLegend. I hope it’s ok to ask you directly, but I re-posted this issue elsewhere as a new scenario: What if I want to open multiple browser tabs and then close each of those tabs, either one by one or all at once?
The problem with that is that it lets you open more than one browser tab, but you can only close the first one you opened. I would like to close each of the new browser tabs opened with that script.
Try the following below, i have tested it and it worked fine for me.
<!DOCTYPE html>
<html>
<head>
<title>Javascript new tab/window</title>
<meta charset="utf-8">
<script type="text/javascript">
//<![CDATA[
var windows = [], i = 0;
function manageWindows(method) {
switch(method) {
case 'open':
var url = prompt('Please enter a valid URL to open in a new tab/window', '');
if (url) {
// Check if the URL contains "http://" in the string
// If it doesn't force "http://" to the front of the URL
if (!url.match(/^(http(s)?):\\/\\//i)) {
url = 'http://' + url;
}
// Make sure the url has a length of at least 1 or else
// do nothing
if (url.length) {
windows[i] = window.open(url, 'tabWindow_' + i);
i++;
}
} else {
alert('No URL was entered');
}
break;
case 'close':
var id = prompt('Please enter the id of the tab/window you would like to close', '');
if (id) {
// Make sure the id is valid
if (typeof windows[id] === 'undefined') {
alert('You have entered an invalid tab/window id!');
return false;
}
windows[id].close();
delete windows[id];
}
break;
default:
alert('Invalid method passed, must be either "open" or "close"');
}
return false;
}
//]]>
</script>
</head>
<body>
<a href="javascript:void(0)" onclick="manageWindows('open')">Create a new tab/window</a> ·
<a href="javascript:void(0)" onclick="manageWindows('close')">Close a tab/window</a>
</body>
</html>
Here is a way to do it. The script stores the object references to the new windows in an array. These windows can then be closed by using
A[number].close();
Make sure you are not blocking pop-ups, or you may not see the new windows as they are created.
[HIGHLIGHT=“”]
<!doctype HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=windows-1252”>
<title>Closing sub windows</title>
<script type=“text/javascript”>
<!–
var A=new Array(); // global
function newPg()
{ var nextW, i;
for(i=1;i<4;i++)
{ nextW=“a”+i;
A[i]=window.open(“”,nextW,“status,menubar,height=400,width=400”);
A[i].document.body.innerHTML=“Page “+i;
}
//
// create links to new pages on parent page
var build=””;
for(i=1;i<4;i++){build+=‘<p class=“curs” onclick="A[’+i+'].close();this.style.visibility=\‘hidden\’">Close Page ‘+i+’<\/p>
'; }
document.body.innerHTML=build;
window.focus();
}
// -----------
window.onload=newPg;
//–>
</script>
<style type=“text/css”>
<!–
body { font-family:arial,helvetica,sans-serif; font-weight:bold; color:#000; }
.curs { cursor:pointer; }
–>
</style>
</head>