Skip to content

Instantly share code, notes, and snippets.

@oraricha
Created August 29, 2013 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oraricha/6380306 to your computer and use it in GitHub Desktop.
Save oraricha/6380306 to your computer and use it in GitHub Desktop.
Invalidate session on tab or window close (not tested!!)
/*JS code begins here*/
/*Global js variable to decide whether to call session invalidate function*/
var validNavigation = false;
//Called when page loads initially
jQuery(document).ready(function() {
//Call to wireupEvents
wireUpEvents();
});
//Function called when the page loads
function wireUpEvents() {
window.onbeforeunload= function() {
if (!validNavigation) {
/*This JS function calls my managed bean method to invalidate session.*/
windowCloseJsFunction();
}
}
// Attach the event keypress to exclude the F5 refresh
jQuery(document).bind('keypress', function(e) {
if (e.keyCode == 116) {
alert('116');
validNavigation = true;
}
});
// Attach the event click for all links in the page
jQuery("a").bind("click", function() {
alert('click a');
validNavigation = true;
});
// Attach the event submit for all forms in the page
jQuery("form").bind("submit", function() {
alert('form');
validNavigation = true;
});
// Attach the event click for all inputs in the page
jQuery("input[type=submit]").bind("click", function() {
alert('input');
validNavigation = true;
});
//Attach button click for all inputs in the page
jQuery("input[type=button]").bind("click", function() {
validNavigation = true;
});
}
/*JS code ends here*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment