To execute Flex cleanup function when browser is closed by user

前端 未结 3 671
忘掉有多难
忘掉有多难 2021-01-14 08:01

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

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 08:54

    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();
    }
    

提交回复
热议问题