早上起來看到在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都寫這樣的沒有經過自己測試的文章,就發布到我覺得如同聖經般的網站上去嗎?比起院子裡的呂震宇和老趙等大俠,真是要汗一汗了....比起國外的開發人員,園子裡的小兄弟們算幸福的了