Dowemo


Question:

i have a problem.

I am working on a chatting application. I want to kill the session if user closes the browser window without logging off. I used 'beforeunload' function but it also fires when a postback event is fired so it's not good for me.

Please help if anyone have any idea about it.


Best Answer:


As you said the event window.onbeforeunload fires when the users clicks on a link or refreshes the page, so it would not a good even to end a session.

However, you can place a JavaScript global variable on your pages to identify actions that should not trigger a logoff (by using an AJAX call from onbeforeonload, for example).

The script below relies on JQuery

/*


* autoLogoff.js


*


* Every valid navigation (form submit, click on links) should


* set this variable to true.


*


* If it is left to false the page will try to invalidate the


* session via an AJAX call


*/


var validNavigation = false;



/*


* Invokes the servlet /endSession to invalidate the session.


* No HTML output is returned


*/


function endSession() {


   $.get("<whatever url will end your session>");


}



function wireUpEvents() {



  /*


  * For a list of events that triggers onbeforeunload on IE


  * check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx


  */


  window.onbeforeunload = function() {


      if (!validNavigation) {


         endSession();


      }


  }



  // 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;


  });



}



// Wire up the events as soon as the DOM tree is ready


$(document).ready(function() {


    wireUpEvents();  


});


This script may be included in all pages

<script type="text/javascript" src="js/autoLogoff.js"></script>


Let's go through this code:

  var validNavigation = false;



  window.onbeforeunload = function() {


      if (!validNavigation) {


         endSession();


      }


  }



  // 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;


  });


A global variable is defined at page level. If this variable is not set to true then the event windows.onbeforeonload will terminate the session.

An event handler is attached to every link and form in the page to set this variable to true, thus preventing the session from being terminated if the user is just submitting a form or clicking on a link.

function endSession() {


   $.get("<whatever url will end your session>");


}


The session is terminated if the user closed the browser/tab or navigated away. In this case the global variable was not set to true and the script will do an AJAX call to whichever URL you want to end the session

This solution is server-side technology agnostic. It was not exaustively tested but it seems to work fine in my tests

PS: I already posted this answer in this question. I am not sure I should answer multiple questions that are similar or post a reference?




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs