-1

I'm trying to call an ajax before user leaving a page, this what i have done so far. But it doesn't even hit the ajax page. This is what i have done so far.

window.onbeforeunload = closeIt();
function closeIt()
{
    var key="save-draft";

            $.ajax({
                url: "app/ajax_handler.php",
                type:"GET",
                data:{key:key},
                success: function(data)  {
                    return data;
                }
            });

}

I Have tried this one also both failed in my case.

$( window ).unload(function() {});
hafiz muhammed
  • 187
  • 1
  • 1
  • 7

1 Answers1

0

The only way I think is to let the user know that it's a process on background with a confirm message, that will block the exit until user click on Accept or you've got the response.

Something like that:

window.onbeforeunload = closeIt();
function closeIt()
{
    /*var key="save-draft";

            $.ajax({
                url: "app/ajax_handler.php",
                type:"GET",
                data:{key:key},
                success: function(data)  {
                    return data;
                }
            });*/
  
      setTimeout(function() {
                                return confirm("There is a process that isn't finished yet, you will lose some data. Are you sure you want to exit?");
                        }, 1000);

}
Jordi Flores
  • 2,080
  • 10
  • 16