views:

656

answers:

3

Hello, everyone.

We have that Flex app talking to the server. We need some kind of *FlexEvent.ON_BROWSER_WINDOW_CLOSE* event but, unfortunately Flex does not provide such.

So I've got the advice to catch a javascript "onbeforeunload" event and call the flash client thru the ExternalInterface-registred method.

Is there any way to subscribe for a javascript event without any javascript code?

Update I'm want to do that is multiply flash app hosting pages. Certainly we could manage that by external javascript file, but it's unclear still...

+2  A: 

You can use ExternalInterface to call Javascript functions defined in the container. See this.

This post describes a situation similar to yours. Take a look.

dirkgently
This is totally we are doing. However it requires browser javascript to be in hosting page.
Artem Tikhomirov
If javascript is not enabled, will you still get the event you want? And if it is, why not put in a line or two as well?
dirkgently
A: 

Sorry, there is no way to do what you are looking for without using Javascript and ExternalInterface.

cliff.meyers
+1  A: 

You can compile the js code inject into your SWF file:

package
{

import flash.display.Sprite;
import flash.external.ExternalInterface;
import flash.net.SharedObject;


public class CloseTest extends Sprite
{
    private var so:SharedObject = SharedObject.getLocal("hello");

    public function CloseTest()
    {
        if (ExternalInterface.available)
        {
            ExternalInterface.addCallback("onclose", onClose);
            ExternalInterface.call(this.javascript);
        }

        this.so.data.count ||= 0;

        trace(so.data.count);
    }

    private function onClose ():void
    {
        this.so.data.count++;
        this.so.flush();
        this.so.close();
    }

    private var javascript:XML = <javascript><![CDATA[function ()
    {
        window.onbeforeunload = function ()
        {
            alert("hello");
            document.getElementById("CloseTest").onclose();
        }

    }]]></javascript>;
}

}
Cotton