Search code examples
ajaxonbeforeunload

why unload and beforeunload not working on firefox? is there any solution?


On Chrome its working properly but on firefox its not working

function myfoo(){
         // Write your logic here
           $.ajax({
            type: "POST",
            url: "update.php",
            dataType: 'json',
             data: {Eventid: 'Eventid',Seats:'Seats'},
            success: function (r) {}

});
    }

When unloading window

$(window).on('beforeunload', function () {
   return 'Are you sure you want to leave?';
});

when unloading call function

$(window).on('unload', function () {
   console.log('calling ajax');
   myfoo();
});

Solution

  • Both "onbeforeunload" and "unload" is working but in your scenario you are expecting to send ajax call. so try changing ajax asynchronous to false.

    function myfoo(){
             // Write your logic here
               $.ajax({
                type: "POST",
                url: "update.php",
                dataType: 'json',
                async: false,
                 data: {Eventid: 'Eventid',Seats:'Seats'},
                success: function (r) {}
    
    });
        }