5 Replies Latest reply on Oct 24, 2007 8:32 AM by msabljic

    Detecting browser window close event?

    msabljic
      Is it possible to detect in Flex application browser window close event so that an action can be started when user closes Flex application, does anyone know how to do that if it's possible in the first place? The reason why i am asking this is because i have a multiuser Flex application where every user has it's own directory on a server side. Application has logout button which triggers cleanup of user's directory but what if the user just closes the window? I would like to be able to lunch that same cleanup upon browser close window.

      thanks in advance
        • 1. Re: Detecting browser window close event?
          kcell Level 2
          In general it is always better to do clean up on the server side. (connection can break cause of 1000 reasons and just one is that the browser is closed)

          In webservices it is the session which runs than on a defined timeout and does the clean up of the session.

          best regards
          kcell

          • 2. Re: Detecting browser window close event?
            msabljic Level 1
            I see what you mean, i am using HTTPService for communication with server side php when needed. What would be then the best option to do this, do you maybe have some example or useful links?

            thanks a lot
            • 3. Re: Detecting browser window close event?
              JKohn99 Level 1
              It is still useful in some cases to prompt the user when they either try and close the browser or navigate to a new page. It is really very simple.

              1) Add the following java script function to your html wrapper page:
              window.onbeforeunload = function()
              {
              try
              {
              var flex = document.${application} || window.${application};
              // promptForClose() is implemented in the Flex application.
              // It returns a string which causes the js function to throw up
              // a "are you sure" yes/no dialog. Note that once the dialog is up
              // you lose control and the browser takes over
              var w = flex.promptForClose();

              if (w!="")
              {
              return w;
              }
              else
              {
              return;
              }
              }
              catch(err)
              {
              window.alert(err.description);
              }
              }
              note, that if you want this on all your flex wrappers put it in the index-template.html in Flex builder.

              2) use the external interface to add a js call back into your Flex application and implement the method.

              if(ExternalInterface.available)
              {
              ExternalInterface.addCallback("promptForClose",promptForClose);
              }

              in your Flex code implement the method:

              private function promptForClose():String
              {
              // do some stuff
              // return an empty string for not further prompting
              // return a message to have the browser to display a dialog.
              }

              Also note that if your using Flex 3 you can use the FABridge which gives your more capabilities. We do not use FABridge because it was just a beta in Flex 2.0

              • 4. Detecting browser window close event?
                kcell Level 2
                here is a link about
                Session in PHP, but I am not a php expert (its also some years ago).

                If you got a "login" function you can start the session there (in the php script and register the callbacks ).
                You have to modify every php script with a check if the session is valid.

                In your flex app you should consider:
                - What happen if timeout occured and you flex app is still open (for example if user went to lunch )

                best regards
                kcell

                PS: @ JKohn99 cool solution if browser/network is still active

                • 5. Re: Detecting browser window close event?
                  msabljic Level 1
                  Thank you very very much both JKohn99 kcell, you've been extremely helpful. I managed to pull this off by using JKohn99 advice with JavaScript but will now start reading on sessions like you kcell suggest since that would be the proper way.

                  best regards