开发者

Requiring confirmation from user before browser close _iff_ a Flex variable is true

开发者 https://www.devze.com 2022-12-20 22:01 出处:网络
I have a Flex application which allows the user to edit a cloud-based document. (Think SlideRocket.) When the user tries to navigate away or close the browser window, I\'d like to show them an are-you

I have a Flex application which allows the user to edit a cloud-based document. (Think SlideRocket.) When the user tries to navigate away or close the browser window, I'd like to show them an are-you-sure dialog iff they have unsaved changes.

I'm using the following custom class, which I found at Flash player notified on browser close or change page (as3). I don't think it is the problem.

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);
        }
    }
}

In my applicationComplete function, I add an event listener to the javascript window.onbeforeunload event, as follows:

ExternalInterfaceUtil.addExternalEventListener("window.onbeforeunload", requestUnloadConfirmation, "unloadConfirmation");

The Actionscript function requestUnloadConfirmation (below) is successfully called when the user tries to close the browser window. However, it does not prevent the browser from closing. (In Chrome, the browser closes and the Actionscript function is called subsequently. In Firefox, the browser stays open for th开发者_StackOverflowe duration of the function but then closes.)

private function requestUnloadConfirmation():String {
    if (changedSinceSave)
        return "There are unsaved changes. Are you sure you want to leave without saving?";
    else
        return null;
}

Behavior is identical in both debug and release builds, and on the production server as well as the local machine.

Any help would be greatly appreciated,

Dave


Right now, when the JavaScript event is fired, it is set to call your function in your AS3 code, which it does. The JavaScript function, however, is not returning the value that your AS3 function returns. To get this behaviour, add 'return' to the JavaScript event-handling function created in addExternalEventListener like so:

var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){return "+jsExecuteCallBack+"};}";

Since the event handler should return a true or false value, your requestUnloadConfirmation function should have a return type of Boolean and return false to cancel the event, and true otherwise. Use the following to get a confirmation dialog box:

private function requestUnloadConfirmation():Boolean {          
    if (changedSinceSave)          
        return ExternalInterface.call("confirm", "There are unsaved changes. Are you sure you want to leave without saving?");
    else          
        return false;          
}

UPDATE: It turns out that returning a string to window.onbeforeunload causes a confirmation dialog box to be shown automatically. The ExternalInterface.call to confirm causes a second dialog box to show; it is redundant. The only change required in the AS3 code is to add the "return" in the generated JavaScript.


In a regular html/javascript web-app you would use the window.onbeforeunload event to do this.

https://web.archive.org/web/20211028110528/http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

Perhaps you can use this event, and check some value of your flex app to determine if you should ask the user (not familiar with flex...)?


I had to run some modifications in the above code to make it work. The problem was due to the fact that the function which handles the event window.onbeforeunload, should not return any value to avoid popup confirmation and should return text value when a popup confirmation is in-order

Here are my changes:

private function requestUnloadConfirmation():String {          
             if (changedSinceSave){
                    return "There are unsaved changes. Are you sure you want to leave without saving?";
             } 
            return null;          
        }

And a little change in embedded JS

var jsBindEvent:String = "function(){"+qualifiedEventName+"= function(){ if ("+jsExecuteCallBack+") return "+jsExecuteCallBack+"};}";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号