-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
  • 25,055
  • 8
  • 49
  • 108
Mozinor
  • 85
  • 1
  • 9

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,163
  • 1
  • 20
  • 22