开发者

I want to detect when user close browser window?

开发者 https://www.devze.com 2022-12-10 18:01 出处:网络
I have Example and i need to kill the session when user close his browser window. i tried page_unload() not working.

I have Example and i need to kill the session when user close his browser window. i tried page_unload() not working. the example is:i have parent windows and w开发者_JS百科indow will open from it i need to delete session when user close the child window. please i need some help.


This is not something that's provided for in the normal web http protocol. There's no real way to know for sure when the browser closes; you can only "sort of" know. You have to throw together an ugly hack to get any level of certainty and even then it's bound to fail in plenty of edge cases or cause nasty side effects.

The normal work-around is to send ajax requests at intervals from the browser to your server to set up a sort of "heartbeat". When the heartbeat stops, the browser closed and so you kill the session. But again: this is a horrible hack. In this case, the main problems are that it's easy to get false positives for a failed heartbeat if the server and client to get out of sync or there's a javascript error, and there's a side effect that your session will never expire on it's own.


You can do this with an window.onbeforeunload handler that posts back to a sign out page.

function CloseSession( )
{
    location.href = 'SignOut.aspx'; 
}
window.onbeforeunload = CloseSession;

This is the duplication question.. chk out here

Closing the browser should actually clear the session variables an ASP.net session id

https://stackoverflow.com/questions/920042/kill-server-side-session-on-browser-window-close-closed


Unfortunately this is not possible. You can find examples on the web claiming that they can do that, but they are flawed sometimes in a very subtle way.


There is no consistent way to detect the browser closing through javascript. If you are concerned about having too many sessions hanging, reduce the session timeout on your server. If you do that you will also want to notify your users that they need to remain active in order not to lose any work.


If your client-side script sent an AJAX heartbeat to your server-side app, you might use a missing beat to close the session.


You have reference of all child windows in parent window you need onfocus to check for child windows so when your parent get focus check if child is there or not

0

精彩评论

暂无评论...
验证码 换一张
取 消