且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户关闭浏览器(不刷新)时如何运行服务器端功能?

更新时间:2023-11-27 22:09:04

您可以使用onbeforeunload事件运行一个JavaScript,该JavaScript告诉您的服务器用户已关闭网站.
you can use the onbeforeunload event to run a javascript which tells your server that the user closed the website.
<body onbeforeunload="alert('User is closing my website :'(');">



要通过javascript与服务器通信,请参见使用Ajax.NET Framework通过JavaScript调用服务器端代码 [ ^ ]



To communicate with your server via javascript see Call Server Side Code by JavaScript using Ajax.NET Framework[^]


我不相信我曾经见过跨浏览器版本的东西.

***的选择是创建一个登录cookie,该cookie在浏览器关闭时到期.当用户访问此站点时-即使他们已登录"-并且cookie不存在,您也可以强制注销.

另外,使用户痛苦地知道应该注销,如果这很重要,则使查找变得很容易.

干杯.
I don''t believe that I''ve ever seen a cross-browser version of something that works.

The best bet is to create a login cookie that expires when the browser closes. When the user hits this site - even if they are ''logged in'' - and the cookie doesn''t exist, you can force the logout.

Also, make it painfully obvious to users that they should log out, and if it''s that important, make it easy to find.

Cheers.


您需要使用javascript的帮助,请在
上调用以下函数 onbeforeunload()

you have to take help of javascript, call following function on
onbeforeunload()

function handleWindowClose()
         {
            if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
                event.returnValue = "Browser Closed";
            }
        }


然后在调用AJAX以执行方法后面的代码之后.


then after call AJAX to execute code behind method.