Reputation: 1184
I've been trying to solve how to close session when browser/tab is closed. Following the Daniel Melo's answer + setting session's coockie TTL to 0 i'm almost ready, but Daniel's tip is not working for input type=[button].
var validNavigation = false;
function endSession() {
document.cookie = 'MYOWNSESSID=; path=/';
}
function okEvent(){
validNavigation = true;
}
function wireUpEvents() {
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
$("a").bind("click", okEvent );
$("form").bind("submit", okEvent );
// that's my adds that don't work
$(":button").bind("click", okEvent );
$(":button").bind("submit", okEvent );
$(":button").onclick= okEvent;
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
The key if to flag an event as valid before unloading occurs. I'm trying to hook the buttons click but without success. The jquery selector is working fine, i checked that $(':button').length is > 0. Sure i'm missing something but can't find it.
Upvotes: 1
Views: 12023
Reputation: 38135
Ok, here are some tips:
-replace the three lines you've added with:
$(':button').click(okEvent);
-Don't bind the submit event on a Button input, not to be confused with the Submit input (more):
<input type="submit" value="Submit" />
-if you are using the Button to submit the form, then use the submit input instead and remove all your additions since the submit event is already attached to the form:
$("form").bind("submit", okEvent );
Please check this test to better understand the Button input.
Upvotes: 2