views:

842

answers:

5

Hi, I need to detect browser/tab closure from within a flex application in order to delete the session information in the server.

I'm familiar with JavaScript events but I was wandering if there is an event in flex for application unload or something of that sort ...

A: 

Sadly Flex/Flash doesn't have any event like this. Probably the best you can do is have some keepalive your flex app sends to the server, and once the server doesn't receive that keepalive for a given amount of time it assumes the SWF was closed and deletes the session info.

Herms
A: 

As answer #1 stated, flash has no way to detect the user closing the browser/tab. However, you might be able to use JavaScript to register for the event and then signal Flex/Flash. At this point however, you might just want to issue a JavaScript Async request to the server to kill the users session. The request isn't guaranteed to complete, but it usually will if the server is fast!

mimetnet
Most likely you won't have time for the javascript message to the SWF to be fully processed before the window closes, so it would be unreliable. So even if you implement that it would still be useful to have the server able to detect things on its own with some kind of timeout.
Herms
+1  A: 

there is another question like yours here that has an answer.

http://stackoverflow.com/questions/1119554/flash-player-notified-on-browser-close-or-change-page-as3

TheBrain
+1  A: 

In case this hasn't already been answered, there is the 'closed' property of the HTML DOM Window object:

'The closed property returns a Boolean value that specifies whether the window has been closed.'

http://www.w3schools.com/htmldom/prop%5Fwin%5Fclosed.asp

You can use externalInterface to call Javascript from Flex

Mark
A: 

Thank you all, I guess I'll go with the solution TheBrain suggested. With the lack of support for that feature in flex, this is the cleanest solution I've seen.

Ofir