Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Browser Close on Asp.net

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

like image 672
Prorammer81 Avatar asked Dec 01 '09 06:12

Prorammer81


2 Answers

<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.

like image 116
Muhammad Hasan Khan Avatar answered Nov 04 '22 01:11

Muhammad Hasan Khan


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.

like image 42
RickNZ Avatar answered Nov 03 '22 23:11

RickNZ