-4

Hi I need to confirm with ajax before leave the page but with confirm('message') I have a bug

$( window ).on( "beforeunload", function(){
    confirm('message');
    $.ajax({
        type: "GET",
        async: false,
        url: 'page.php',
        success: function(dat) {
            alert(dat);
        }
    });
});

You have a idea for confirm before ajax?

David Mulder
  • 26,123
  • 9
  • 51
  • 114
Mozinor
  • 85
  • 1
  • 10
  • please add more detail of problem, btw if you don't test the return value of confirm the confirm its useless – ale Sep 06 '14 at 17:29
  • this is just not a reliable way to do server communications, do it long before unload – charlietfl Sep 06 '14 at 17:32

1 Answers1

0

confirm() returns a boolean. You need to check it's value before continuing.

if(confirm("Do you want to AJAX something?") {
$.ajax({
            type: "GET",
            async: false,
            url: 'page.php',
            success: function(dat) {
                alert(dat);
            }
        }); 
}
Design by Adrian
  • 2,185
  • 1
  • 20
  • 22