在Asp.net上检测浏览器关闭

时间:2022-07-13 03:36:03

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

我想在注销时执行一些功能,如果用户直接关闭了他的浏览器,那么相同的功能想要做,我们无法在页面卸载,因为我的网站有超过100个页面,因为这将适用于每个页面的重定向页

Thank You

3 个解决方案

#1


15  

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

使用javascript + jquery关闭窗口时调用logout.aspx。在logout.aspx页面加载事件中执行任何您想要执行的操作。

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

上面的代码片段必须添加到母版页的html中。

#2


7  

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.

不幸的是,如果用户关闭浏览器,则没有可靠的通知方式。除了客户端页面卸载,你已经说过你不想要的,唯一的另一个选择是定期基于Ajax的轮询;两者都是丑陋的,而且出了名的不可靠。

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

通常,服务器端超时是一种更好的方法。

#3


1  

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

这不是global.asax中Session_OnEnd的用途吗?

This event isn't triggered when the browser is closed.

关闭浏览器时不会触发此事件。

#1


15  

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

使用javascript + jquery关闭窗口时调用logout.aspx。在logout.aspx页面加载事件中执行任何您想要执行的操作。

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

上面的代码片段必须添加到母版页的html中。

#2


7  

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.

不幸的是,如果用户关闭浏览器,则没有可靠的通知方式。除了客户端页面卸载,你已经说过你不想要的,唯一的另一个选择是定期基于Ajax的轮询;两者都是丑陋的,而且出了名的不可靠。

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

通常,服务器端超时是一种更好的方法。

#3


1  

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

这不是global.asax中Session_OnEnd的用途吗?

This event isn't triggered when the browser is closed.

关闭浏览器时不会触发此事件。