Magnific-popup: Magnific Popup Callback when closing

Created on 19 Jun 2013  路  5Comments  路  Source: dimsemenov/Magnific-Popup

Is there anyway to get confimation when user click or trying to close the Popup. This is my demo http://jsfiddle.net/K9Geq/

If user click cancel the Popup should remain as it is, Or if user click Ok, We need to close the Popup.

Most helpful comment

I'm just overriding close method in MagnificPopup object that is created when open callback is fired:

// this part overrides "close" method in MagnificPopup object
$.magnificPopup.instance.close = function () {

    if (!confirm("Are you sure?")) {
        return;
    }

     // "proto" variable holds MagnificPopup class prototype
     // The above change that we did to instance is not applied to the prototype, 
     // which allows us to call parent method:
     $.magnificPopup.proto.close.call(this);
};

You can override any function in this manner...

All 5 comments

Possible with some JavaScript magic http://jsfiddle.net/K9Geq/1/

@dimsemenov man you saved my time... thanks.. :) can you explain what did you just did

I'm just overriding close method in MagnificPopup object that is created when open callback is fired:

// this part overrides "close" method in MagnificPopup object
$.magnificPopup.instance.close = function () {

    if (!confirm("Are you sure?")) {
        return;
    }

     // "proto" variable holds MagnificPopup class prototype
     // The above change that we did to instance is not applied to the prototype, 
     // which allows us to call parent method:
     $.magnificPopup.proto.close.call(this);
};

You can override any function in this manner...

Thanks. Now I understand how its working... :D

@dimsemenov This doesn't seem to work when calling the popup using the open method (or perhaps when using AJAX).

$.magnificPopup.open({
    items: {
        src: '/path/to/file',
        type: 'ajax',
    },
    callbacks: {
        open: function() {
            $.magnificPopup.instance.close = function() {
                console.log('close override is working');
                $.magnificPopup.proto.close.call(this);
            }
        },
        ajaxContentAdded: function() {
            var m = this;
            this.content.find('.closebutton').on('click', function(e) {
                e.preventDefault();
                m.close();
            });
        }
    },
    closeOnContentClick: false,
    closeOnBgClick: false,
    showCloseBtn: false,
    enableEscapeKey: true
});

In this example, the console.log happens, but the window never closes. I've also tried moving the override of the close method out of the popup call completely, same result. What might I be doing wrong?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

despecial picture despecial  路  5Comments

wojto picture wojto  路  4Comments

landitus picture landitus  路  3Comments

BorislavZlatanov picture BorislavZlatanov  路  4Comments

Madeirense picture Madeirense  路  6Comments