早上起来看到在google订阅中看到aspalliance的一篇文章,立即饶有兴趣地阅读起来,题目的标题是:
Handle Browser Close Event on the Server-Side
意为在服务器端捕获浏览器关闭事件。这个技术是一个众所周知的难题,我想看看他是怎么解决的。结果看下去原来使用的body的onunload事件,触发asp.net ajax 的pagemethod来释放服务器端的资源,很奇怪,这样可以吗?当刷新postback,都回导致onunload事件的触发,难道你都要释放一次资源不成?结果在评论的第一条:
This article seemed promising, but unfortunately the javascript chain of events is going to cause undesired results.
第二条评论:
Unfortunately, the code in this article will not work. The onunload event fires every time you leave a page, so you'll be abandoning the session all the time.
接下来的几条评论也都是关于这个does not work的问题,不过倒是有人给了几个不起作用的原因:
1) The user has javascript disabled
2) The user's browser doesn't support javascript
3) The user kills the browser process
4) The user turns off their machine or has a power cut
也有人出了奇招:
One proposed solution is to use the mouse pointer to locate the window close button "X". Off course this will be purely based on the mouse movement but then you should also consider the chances of user resizing the window and then closing it. So, basically what is required is to get the mouse coordinates based on the openned browser window.
总之是不行啦,因为:The problem with onunload is that it will be fired even if you refresh the page or fire an ASP.NET postback event from any server side button.
不过有人提出的解决办法是:
Another option for this case is that you use a keep alive functionality in your web application. after implementing keep alive, u can keep your session expire time to a short interval, say 1 or 2 mins. through this u can check for the session after every 1 or 2 mins and abandon it accordingly
不过真是汗一个:国外的mvp都写这样的没有经过自己测试的文章,就发布到我觉得如同圣经般的网站上去吗?比起院子里的吕震宇和老赵等大侠,真是要汗一汗了....比起国外的开发人员,园子里的小兄弟们算幸福的了