3

I want to destroy the session on tab close using jquery and java script. In this I am able to track the keyboard event like F5 or ctrl+r. But I am facing issues in tracking browser back, forward, refresh and tab close buttons.I know that on all the above mention button, same function is called that is window.onbeforeunload.

Here is my code..

function endSession() {
  console.log(validNavigation);
  alert("You are about to close your page and session");
    $.ajax({
        type: "POST",
        url: base_url+"destroy-session",

        beforeSend : function(){
            console.log('hjkl');    
        },
        success: function(response){
            console.log(response);
        },
        error: function(response){
            console.log(response);
        }
});     

} 


function wireUpEvents() {
window.onbeforeunload = function() {      

  if (validNavigation == false) {
     endSession();
  }
}  
//window.onbeforeunload=goodbye  

// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress keydown keyup', function(e) {
    if(e.keyCode === 116 || e.keyCode==8 || e.keyCode==13) {
    validNavigation = true;
    }
    if(e.ctrlKey && e.keyCode==82) {
        validNavigation = true;
        }    
    if(e.ctrlKey && e.keyCode==76) {
        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;
});

$("input[type=button]").bind("click", 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();
});
  • What exactly is problem? Like you said, `onbeforeunload` is executed when user clicks back or closes tab. – marekful Dec 21 '15 at 11:28
  • The problem is that session is getting destroyed even when I click on browser back, forward and refresh button. I want to destroy session only one user closes the tab. – Akash Patni Dec 21 '15 at 11:38
  • 1
    You should take a different approach. Destroy session on server side. Track user activity and close the session after a certain time the user is inactive. Most probably you _don't_ need to destroy the session immediately when user navigates away. For that, there is the logout. – marekful Dec 21 '15 at 11:42
  • But what if I want to destroy session on tab close just like bank websites do. Is there any way to resolve this?? – Akash Patni Dec 21 '15 at 11:50
  • Banking sites don't do that. What they do is to implement a session timeout, display the user a message after some time of inactivity asking them if they wish to stay logged in. If there's no response to that question, they execute the logout functionality on the client side (which in turn destroys the session on the server). At the same time, the same inactivity timeout is implemented on the server. If the user closes the browser (so they don't have the chance to see and respond to the message) and the specified amount of time has passed, the server itself destroys the session. – marekful Dec 21 '15 at 11:54
  • @marekful bank websites do close the session as soon the tab is closed, there is no wait for any duration. I am also looking for a solution to close the session as soon the tab is closed and not wait for timeout. – archana Mar 08 '16 at 22:42

0 Answers0