I have a Flex client application. I need a clean up function to run in Flex when the user closes the browser. I found the following solution on the net, but it only works ha
Unfortunately, there is no solid way of doing such clean up functions that execute asynchronously. The result
/fault
events of the HTTPService occur asynchronously after the cleanUp
method is returned. The browser waits only till the onbeforeunload
function (the js clean_up function) returns. Unless you call event.preventDefault()
from that function, the page will be closed. Note that calling preventDefault() will result in an ok/cancel popup asking:
Are you sure you want to navigate away from this page?
Press OK to continue, or Cancel to stay on the current page.
If the user selects OK, the browser will be closed nevertheless. You can use the event.returnValue
property to add a custom message to the popop.
//tested only in Firefox
window.addEventListener("beforeunload", onUnload, false);
function onUnload(e)
{
e.returnValue = "Some text that you want inserted between " +
"'Are you sure' and 'Press OK' lines";
e.preventDefault();
}