I need to clear the session variables when the tab is closed but I could not find any solutions so far. The closest I have got is by using the "onbeforeunload" function is javascript.
<body onbeforeunload='destroySession()'>
<!--Codes for the site includeing php scripts-->
</body>
<script type='text/javascript'>
function destroySession(){
$.ajax({
url: "destroySession.php"
});
}
<script>
The problem is the function is called every time a new link is clicked, refreshed or even if a form is submitted. Is there a better way to destroy session variables on closing a tab or am I doing something wrong? Please help.
I need to clear the session variables when the tab is closed but I could not find any solutions so far. The closest I have got is by using the "onbeforeunload" function is javascript.
<body onbeforeunload='destroySession()'>
<!--Codes for the site includeing php scripts-->
</body>
<script type='text/javascript'>
function destroySession(){
$.ajax({
url: "destroySession.php"
});
}
<script>
The problem is the function is called every time a new link is clicked, refreshed or even if a form is submitted. Is there a better way to destroy session variables on closing a tab or am I doing something wrong? Please help.
Share Improve this question asked Aug 16, 2013 at 14:54 BhunsBhuns 511 gold badge1 silver badge5 bronze badges 8- 7 Just trust me, stop. You're not going to be able to do this reliably and pletely. You can detect how the page is being navigated away from in some ways (clicking a link, submitting a form, etc.), but you won't be able to distinguish between "closing the tab" and "navigating" and many other events. It's unfortunate that we can't do this kind of thing (because it would be nice to clear a session like you ask), but things aren't set up to work properly. Remember, this is a website, not a desktop application – Ian Commented Aug 16, 2013 at 14:56
- possible duplicate of Close/kill the session when the browser or tab is closed – Jamie Taylor Commented Aug 16, 2013 at 14:56
- 1 Rather than detecting the user leaving the site, do it the correct way and instead time them out after a set period of time *Hint: you don't actually have to do anything to make this happen! using tab close is going to be troublesome when someone opens your application in two tabs at once (which isn't unmon), then decides to close one of the two. – Kevin B Commented Aug 16, 2013 at 14:57
- 1 Tell us 'why' you are trying to do this because there may be a standard practice you are missing out on. – Flosculus Commented Aug 16, 2013 at 14:59
- You may want to use a session cookie: en.wikipedia/wiki/HTTP_cookie#Session_cookie Session cookies are automatically cleared up by the browser when the browser window is closed. This may be good enough for your use case? – DigitalZebra Commented Aug 16, 2013 at 15:03
3 Answers
Reset to default 4There is no secure way of handling what you are looking for. onbeforunload event executes every time you leave the page. (I had similar problem like this yesterday for one of my projects). The closest you can get is to control how the users leave the page.
See this link posted by lan in some of the ments. And check the answer by Daniel Melo That is as close you can get with the solution from this problem.
I also found this link but its basically the extraction of the answers given in stackoverflow.
Hope this helps.
Unfortunately, there is no way to prevent page refresh (caused by form submit or any other navigation) from calling "onbeforeunload".
Yep you can do it,
</head>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
alert("bye");
}
function wireUpEvents() {
/*
* For a list of events that triggers onbeforeunload on IE
* check http://msdn.microsoft./en-us/library/ms536907(VS.85).aspx
*/
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
</script>
</head>
<body>
<h1>Eureka!</h1>
<a href="http://www.google.">Google</a>
<a href="http://www.yahoo.">Yahoo</a>
</body>
</html>