views:

796

answers:

3

I want to do some functionality on log-out, if the user directly closed his browser then same functionality want to do, we can not do on page unload because there are more than 100 pages in my website because this will work on redirection from each page

Thank You

+3  A: 
<script type="text/javascript">
var closing = true;
$(function () {
    $("a,input[type=submit]").click(function () { closing = false; });
    $(window).unload(function () {
        if (closing) {
            jQuery.ajax({ url: "http://localhost:49591/Account/LogOff", async: false });
        }
    });
});
</script>

Call the logout.aspx when window closes using javascript + jquery. Do whatever you want to do in the logout.aspx page load event.

The above snippet will have to be added in your master page's html.

Hasan Khan
+1, sounds reasonable. The browser close has to be done at client-side (JavaScript) since it won't be send back to the server which therefore won't get notified about it.
Juri
But the OP said they didn't want to use page unload, right? One issue is that this event will fire not just when the user closes their browser (although only in *some* browsers), but also when they navigate to the next page.
RickNZ
-1 This does not work as expected
Josh Stodola
Answer modified to cater for links/submit buttons that cause unload due to navigation.
Hasan Khan
Can you please give some code samples
Prorammer81
Shameem you have to give absolute url not a relative one.You will have to disable the browser toolbar too.
Hasan Khan
This solution can cause problems; its pretty normal to open multiple tabs/windows of the same site, so if you do and close one of the tabs/windows, you will be logged out of the other tab/window.
JonoW
He wants to logout the person when the window closes and the problem you mentioned is with what he wants to do because no matter how he achieves this, multiple windows situation will be a problem anyway.
Hasan Khan
+3  A: 

You can have a button for the "logout" case.

Unfortunately, there is no reliable way to be notified if the user closes their browser. Other than client-side page unload, which you've said you don't want, about the only other option is periodic Ajax-based polling; both are ugly and notoriously unreliable.

In general, server-side timeouts are a better approach.

RickNZ
Can you please give some code samples
Prorammer81
+1  A: 

Isn't this what Session_OnEnd in global.asax is for?

Mike Kingscott
Session_OnEnd is not always reliable, only fires for InProc session mode
JonoW
Ok, thanks for the (painful) lesson ;-)
Mike Kingscott