Andy Jacobs
Andy Jacobs

Reputation: 15245

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 ?

Upvotes: 6

Views: 7218

Answers (3)

Nick
Nick

Reputation: 11

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+"};}";

Upvotes: 1

Joel Hooks
Joel Hooks

Reputation: 6565

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...

Upvotes: 15

Justin Niessner
Justin Niessner

Reputation: 245509

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.

Upvotes: 1

Related Questions