问题描述
大家好,
我想在用户关闭浏览器时运行服务器端功能. (不刷新)
我尝试卸载和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?
推荐答案
<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[^]
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.
这篇关于用户关闭浏览器(不刷新)时如何运行服务器端功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!