问题
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");
回答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 );
}
}
回答2:
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
:)
来源:https://stackoverflow.com/questions/4387212/differentiate-between-f5-and-browser-close-event-in-flex-actionscript