4

I am using the following piece of code to determine the closure of browser window and it works perfectly. But the issue arises when the user refreshes the page using "F5" or clicking browser refresh button and even in that case the same piece of code is invoked.

I would like to know if there is anyway by which i can distinguish between browser close event and refresh event using actionscript

package utils
{
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 );
    }
}
}

Call to above utility is done by

ExternalInterfaceUtil.addExternalEventListener("window.onunload", handleLogout, "unloadFlex");

Arun
  • 311
  • 3
  • 7
  • 15
  • I'm pretty sure, the HTML onunload event will fire whenever the user leaves the page. Refreshing the page is, in essence, leaving it and coming right back to the same place. I suspect there is nothing you can do. Why do you care about one way to leave, but not the other? – JeffryHouser Dec 08 '10 at 14:34
  • when i close the browser i want to invoke the logout handler but i dont want the same thing to happen when user refreshes the screen – Arun Dec 09 '10 at 07:37

2 Answers2

1

Pseudo code:

public static function addExternalEventListener( qualifiedEventName:String, callback:Function,callBackAlias:String ):void
{
   if(lastKeyPressed.KeyCode != 116)
   {
    // 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 );
 }
}
Zack Brown
  • 91
  • 4
  • Plus one for a creative solution to the problem, but how do you get the lastKeyPressed in AS3 or JavaScript? How would this deal with clicking the refresh button? What happens when the user presses F5 then closes the browser window? – JeffryHouser Dec 08 '10 at 18:20
  • Actually, now that I'm looking at it, this is a very wrong answer. I would have put the lastKeyPressed check on the handler, not where we are adding the listener. – Zack Brown Dec 09 '10 at 20:20
0

Write to the shared object every few seconds with current local time.

If that exists within (last few seconds + x), it was a refresh rather than close.

Let me know if you need some code examples

:)

Nate
  • 2,881
  • 1
  • 14
  • 14
  • Not sure if I got this one. But the problem was resolved using ChannelSet login API which is provided with BlazeDS. For this we need to define our own loginCommand class under the security tag of services-config.xml, which will actually perform the authentication. On the flex end calling channelset.login() will invoke the doAuthentication API defined in LoginCommand implementation. With every browser close the authentication is reset on the server. – Arun Jul 14 '11 at 04:54