views:

30

answers:

3

Need help on browser close event. I see a tutorial but not working on all browsers. Need a javascript code that works on all browsers. My purpose on that is to delete a php session on browser close.

Thank you

+1  A: 

You can use window.onunload to find out if a user is leaving a page/closing a window/closing a tab.

But there is no way to really find out if a user has left your website. He might still have another tab or window opened.

So your only real solution is to check how much time has passed since the last activity from that user. And if that has to happen live, you could add an ajax-like callback to poll the server every minute or so to be sure that the user still has his window open.

WoLpH
thank you, I'll try it.
Jordan Pagaduan
thanks, it works
Jordan Pagaduan
A: 

I don't understand.

Session-length cookies are already automatically deleted when the browser is closed. And the session data on the server won't last any longer than what is defined by sesssion.gc_maxlifetime (24 minutes by default) when the session garbage collector executes.

You shouldn't need this.

Peter Bailey
A: 
<script language=”javascript”>

function deleteSession(){
 // session deletion
}

window.onbeforeunload = deleteSession;
</script>
simplyharsh