views:

1489

answers:

5

I have an HTML wrapper that contains a Flex application, is there an Event that I can listen on, that is triggered when a user leaves the HTML wrapper either by navigation arrows or closing the browser?

Thanks.

A: 

There is Body.onUnload, but i'm not sure how reliable it actually is.

Steve g
A: 

The closest thing I've found for this is the javascript window.onunload event. However, you can't really listen for this within the Flex app, as the app may not be running anymore by the time the unload method is called. We've used it to signal to other parts of the page via javascript that the app was unloaded though, so depending on what you need to do that might be enough.

Herms
+1  A: 

Edit: I realized this may not be what you're asking for exactly, but I'll leave it in in case it's helpful for anyone looking for it. Also note that you can perform other actions in the onbeforeunload event that will generally be reliably executed before the user accesses the confirmation dialog, unless your unload routine is overly complex (in which case you should consider altering your design anyway).


onbeforeunload lets you interrupt page unload:

window.onbeforeunload = function(e) {
    // Browser will pop up a confirmation dialog, with some text before
    // and after your return string; try it in different browsers to
    // see how they behave.
    return 'String to confirm';
}
eyelidlessness
+2  A: 

You can also listen for Event.ACTIVATE and Event.DEACTIVATE in Flash. All EventDispatchers receive these events when Flash/AIR gains or loses focus from the OS.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/EventDispatcher.html#event:deactivate

This is very helpful for when you you provide a link that opens a new window and you want to reduce functionality and load (pause and mute a video for example) and then resume when the user comes back.

RickDT
A: 

The question is, what do you need to do when the user navigates away?

If you need to perform an action on your server, then the best way to handle this is to open a Socket() when your swf initializes, and then when the user navigates away, that socket will be terminated, and the server can detect that and perform additional logic.

If you need to perform a client side operation, like saving a SharedObject, then you can't rely on a "just one last thing" event to the plugin, since there are alot of avenues to closing out a plugin session. In that case, your best bet is to continually be saving SharedObjects every few seconds.

seanalltogether