title: Requiring confirmation from user before browser close _iff_ a Flex variable is true
body: <p>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.</p>
  
  <p>I'm using the following custom class, which I found at <a href="https://stackoverflow.com/questions/1119554/flash-player-notified-on-browser-close-or-change-page-as3">Flash player notified on browser close or change page (as3)</a>. I don't <em>think</em> it is the problem.</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>In my applicationComplete function, I add an event listener to the javascript window.onbeforeunload event, as follows:</p>
  
  <pre><code>ExternalInterfaceUtil.addExternalEventListener("window.onbeforeunload", requestUnloadConfirmation, "unloadConfirmation");
  </code></pre>
  
  <p>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 the duration of the function but then closes.)</p>
  
  <pre><code>private function requestUnloadConfirmation():String {
      if (changedSinceSave)
          return "There are unsaved changes. Are you sure you want to leave without saving?";
      else
          return null;
  }
  </code></pre>
  
  <p>Behavior is identical in both debug and release builds, and on the production server as well as the local machine.</p>
  
  <p>Any help would be greatly appreciated,</p>
  
  <p>Dave</p>