1

I put following script to prevent leave page meanwhile processing steps

<script language="JavaScript">
window.onbeforeunload = confirmExit;

function confirmExit()
{
JQObj.ajax({
    type: "POST",
    url: "<?php echo $this->url(array('controller'=>'question','action'=>'cleaSess'), 'default', true); ?>",
    success: function(data){}
});

return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
}

</script>

But every time i get default alert message instead of i set custom alert message,

And i want to call ajax call when end user click on "Leave page" button, but in above script ajax call calls before the click leave button,

Anyone have idea or logic to call ajax if and only if people leave the page.

MRJethva
  • 357
  • 4
  • 15
  • There are several related questions that can provide you with the information you need. Also, the language attribute is outdated. Use nothing or `type="text/javascript"` – René Oct 15 '12 at 06:27

2 Answers2

2

You can use the "unload" event for sending the AJAX request:

<script type="text/javascript">
    window.onbeforeunload = function() {
        return "You have attempted to leave this page. "
               + "If you have made any changes to the fields without "
               + "clicking the Save button, your changes will be lost. "
               + "Are you sure you want to exit this page?";
    };

    window.onunload = function() {
        // Ending up here means that the user chose to leave
        JQObj.ajax({
            type: "POST",
            url: "http://your.url.goes/here",
            success: function() {}
        });
    };
</script>

See, also, this short demo.

gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • (fail) I just noticed I anwered a 1-year old question (I thought it was asked on Oct 15 201 **3**, not 201 **2**). – gkalpak Oct 21 '13 at 19:45
  • I dont see a fail, as you just tried answering question which doesn't have accepted solution. – instinct Feb 23 '16 at 13:44
-1

You shold try something like this:

window.onbeforeunload = displayConfirm;

function displayConfirm()
{
    if (confirm("You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?"))
    {
        confirmExit();
    }
}
  • This is a not proper logic, b'coz this alert call everytime on page load and i want to alert on page leave,, – MRJethva Oct 15 '12 at 06:11
  • I had incorrectly wrote "window.onbeforeunload = displayConfirm();". See my edited post. –  Oct 15 '12 at 06:21
  • If you have forms in your page you may need to unbind this event too. See http://stackoverflow.com/questions/1631959/browser-window-close-event –  Oct 15 '12 at 06:26
  • now its ok, Now i leave page confirm alert comes, but click on either cancel or ok button of alert, it leaves page, not staying on page. – MRJethva Oct 15 '12 at 06:39
  • You are right this doesn't work. Your code seems to do the job. But have you tried calling the AJAX request at window.onunload while leaving only the message part at onbeforeunload ? –  Oct 15 '12 at 07:03