用户关闭浏览器(不刷新)时如何运行服务器端功能? [英] How to run server side function when user close the browser (not refresh)?

查看:105
本文介绍了用户关闭浏览器(不刷新)时如何运行服务器端功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想在用户关闭浏览器时运行服务器端功能. (不刷新)
我尝试卸载和beforUnload事件,但是刷新页面时也会触发这些事件.
因此,我无法使用这些事件,我只想在用户关闭浏览器时运行该功能.
或者我怎么能预测浏览器是关闭还是刷新?
实际上,我想编写一个代码,当一个用户登录到我的网站时,另一个具有相同用户名的用户只有在第一个用户退出后才能登录.
因此,当第一个用户登录时,我会检查我的数据库,然后在他/她登录后,我会更新一个表.因此,直到用户注销后,我才能再次登录该表.

我也不能使用cookie,因为用户可以删除它们!

有人可以帮助我吗?

Hi,everyBody

I want to run a server side function when user close the browser. (not refresh it)
I try unload and beforUnload events But these events also fire when the page refreshed.
So I can not use these events, I want run the function JUST when user CLOSE the browser.
Or how can I predict is browser close or refresh fired
In fact I want to write a code that when a user log in to my website another user with same username can not log in until the first user log out.
So when first user log in I check my data base and after he/she log in I update the a table So nobody can log in until the user log out that I update the table again.

Also I can not use cookies because user can delete them!

anybody can help me?

推荐答案

您可以使用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.


这篇关于用户关闭浏览器(不刷新)时如何运行服务器端功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆