Magnific Popup Callback when closing

Mifas picture Mifas · Jun 14, 2013 · Viewed 20.3k times · Source

I am using Magnific Popup for uploading images. When the user clicks or presses the close button, I'd like to get confirmation from the user whether to close on not.

This is my Javascript:

$('#upload').magnificPopup({
    type:'inline',
    callbacks: {
        close: function(){
            if( confirm("Are you sure you want to close?") ) {
              return true;
            }
              return false;
            }
          }
      }
});

But it is not working.

Answer

Michael Irigoyen picture Michael Irigoyen · Jun 27, 2014

You can override the close method. By modifying the instance, you will only change the functionality of this specific popup. Then simply call the original close method to finish the job.

$('#upload').magnificPopup({
  type:'inline',
  callbacks: {
    open: function() {
      $.magnificPopup.instance.close = function() {
        // Do whatever else you need to do here
        var confirmed = confirm("Are you sure you want to close?");
        if(!confirmed) {
          return;
        }

        // Call the original close method to close the popup
        $.magnificPopup.proto.close.call(this);
      };
    }
  }
});