Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash player notified on browser close or change page (as3)

I have to detect in my flash if the user closes his browser or goes to another page and the flash is not accessible anymore. How do i achieve that ?

like image 487
Andy Jacobs Avatar asked Jul 13 '09 13:07

Andy Jacobs


Video Answer


3 Answers

ExternalInterfaceUtil.addExternalEventListener("window.onunload", handleLogout, "unloadFlex");

package
{
    import flash.external.ExternalInterface;

    public class ExternalInterfaceUtil
    {
        public static function addExternalEventListener( qualifiedEventName:String, callback:Function,callBackAlias:String ):void
        {
            // 1. Expose the callback function via the callBackAlias
            ExternalInterface.addCallback( callBackAlias, callback );
            // 2. Build javascript to execute
            var jsExecuteCallBack:String = "document.getElementsByName('"+ExternalInterface.objectID+"')[0]."+callBackAlias+"()";
            var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){"+jsExecuteCallBack+"};}";
            // 3. Execute the composed javascript to perform the binding of the external event to the specified callBack function
            ExternalInterface.call( jsBindEvent );
        }
    }
} 

I don't remember where I got this from, but I've used it and it works pretty well. Of course not all browsers are going to cooperate, but it is better than nothing...

like image 180
Joel Hooks Avatar answered Sep 21 '22 12:09

Joel Hooks


You could use a combination of Javascript and Flash to achieve what you're looking for.

Use Javascript to detect when the user navigates away from the page. Use the javascript event to call into your Flash movie using ExternalInterface. Once your code is called, you can handle the event as needed.

like image 36
Justin Niessner Avatar answered Sep 18 '22 12:09

Justin Niessner


The above worked great for me with one exception: if I return null as my string, I don't want any message to pop up. It works for all browsers except IE, which brings up a dialog box that says "null".

That can be corrected by altering one line to add a null check:

var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){if (" + jsExecuteCallBack + ") return "+jsExecuteCallBack+"};}";
like image 25
Nick Avatar answered Sep 22 '22 12:09

Nick