Expand my Community achievements bar.

Confirmation upon browser window close event

Avatar

Level 2

Our web application open a popup browser window contains a cutomized LiveCycle workspace from where the user submit a data form. I want to implement a feature that when the user close the browser window, a message box will pop up for user's confirmaiton (OK to close the window, Cancel to remain in the workspace window). Here is my code:

In javascript:

    <script LANGUAGE="JavaScript">

        window.onbeforeunload = confirmExit;
       
        var needToConfirm = true;
       
        var confirmMsg = "Are you sure you want to exit this page?"
       
        function confirmExit() {
            if (needToConfirm) {
                alert("Event triggered!");
                event.returnValue = confirmMsg;
                //return confirmMsg;
            }
        }

        function setNeedConfirm() {
            needToConfirm = true;
        }
       
        function releaseNeedConfirm() {
            needToConfirm = false;
        }

     </script>

In *.mxml:


     flash.external.ExternalInterface.call("releaseNeedConfirm"); or flash.external.ExternalInterface.call("setNeedConfirm");

This approach works perfectly with IE, but for some reason it does not work with FireFox. I confirmed that the line alert("Event triggered!") is reached in FF, but no confirmation box is popped up.

Anyone can help me here?

Thanks

John

7 Replies

Avatar

Level 10

Just change the line of code:

event.returnValue = confirmMsg;  

as the following;

event.preventDefault = confirmMsg;

Hope this will work!

Nith

Avatar

Level 2

Thank you Nith, but I tried it with no luck, and it stopped working in IE. Anything else I can try?

Avatar

Level 10

The code I mentioned is applicable only to FireFox.

To make your logic work on both browsers, you need to find out the browser type and should handle the event accordingly.

-Nith

Avatar

Level 10

You can detect the browser type by:

var browser=navigator.appName;

var b_version=navigator.appVersion;

var version=parseFloat(b_version);

alert("Browser name: "+ browser);

alert("Version: "+ version);

-Nith

Avatar

Level 2

I did check in both IE and FF but the browser juct closed silently. Did I miss anything in the code?

Avatar

Level 10

I will try to make an example and then let you know.

Nith

Avatar

Level 2

If I put the javascript into a standard HTML, it works fine in both IE and FF. But once I put it in a HTML of a Flex application it stops working with FF.