In my webpage I want to logout a user when s/he closes the window. To capture this event I am using beforeunload event handler. But when I do close the tab the removeCookie is not called.
Here is the sample code. This is a modified code of this SO Question
var preventUnloadPrompt;
var messageBeforeUnload = "my message here - Are you sure you want to leave this page?";
$('a').live('click', function () {
preventUnloadPrompt = true;
});
$('form').live('submit', function () {
preventUnloadPrompt = true;
});
if ($("#refreshFlag").val() == 1) {
preventUnloadPrompt = true;
};
$(window).bind("beforeunload", function (e) {
var rval;
if ($("#refreshFlag").val() == 1) {
preventUnloadPrompt = true;
};
if (preventUnloadPrompt) {
return;
} else {
removeCookie();
window.location = "/";
}
return rval;
})
I am using JQuery 1.5.2.
What I want is when a user refresh the page it should refresh and when the tab is closed the "removeCookie" should be called. What is it that I am doing wrong in the above code?
UPDATE
As the refresh was giving problem this event was dropped from the project.