A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Detecting browser window close event?

  1. #1
    Member
    Join Date
    Jun 2006
    Location
    Croatia
    Posts
    90

    Detecting browser window close event?

    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
    Last edited by blu3; 10-23-2007 at 12:58 PM.

  2. #2
    Member
    Join Date
    Jun 2006
    Location
    Croatia
    Posts
    90
    I managed to pull this off and and here comes the solution. First i declared cleanUp() function in Flex which does the dirty work and cleans user's leftovers. Then i use ExternalInterface for registering my cleanUp() function so that i can access it using JavaScript from .html wrapper:

    Code:
    import flash.external.ExternalInterface;
    ExternalInterface.addCallback("myFlexFunction",cleanUp);
    
    public function cleanUp():void{
    //do the dirty work
    ........................
    .........................
    }
    Then i added following JavaScript function to my .html which embeds Flex application:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    
    window.onbeforeunload = clean_up;
    
    function clean_up()
    {
    var flex = document.${application} || window.${application};
    flex.myFlexFunction();
    }
    </SCRIPT>
    And that's it, works in both Firefox and IE when user either closes browser window or goes to another web page. Alternative solution is using FABridge or better yet proper session handling
    Last edited by blu3; 10-24-2007 at 11:47 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center