views:

97

answers:

1

I am tracking the window close/navigation event thru following code.

window.onbeforeunload = winClose;
    function winClose() {
        if (isDataChanged) {       
            return "Are you sure you want to close?";
        }
    }

This obviously will popup a confirmation with OK / Cancel. when the browser is closed.

Here i want to call an event after OK / Cancel is pressed. How to do that?

+1  A: 

Try:

window.onbeforeunload = winClose;
    function winClose() {
        if (isDataChanged) {
            setTimeout(function() { do_canceled_event();  }, 500);
            return "Are you sure you want to close?";
        }
    }

using setTimeout, you take it out of the normal flow and js on the page is halted while the dialog is open. do_canceled_event() should run once processing continues. Hope that helps.

(note: not tested)

Jonathan Fingland
What if i press OK button?
Madhu
then it depends on how quickly they leave the page. If this is anything critical or obtrusive, then onbeforeunload probably isn't the best place anyways, but if it's simply showing a div to say "Don't forget to save!", then it doesn't matter much.
Jonathan Fingland
also, if the delay is acceptable, just increase the duration on the timer. There is no way to know if the user pressed OK or Cancel... just whether or not the script will execute afterwards
Jonathan Fingland