jackmoore / colorbox

A light-weight, customizable lightbox plugin for jQuery
http://www.jacklmoore.com/colorbox/
MIT License
4.75k stars 1.14k forks source link

Colorbox won't close after redirect back from child colorbox #882

Closed cxanders closed 4 years ago

cxanders commented 4 years ago

I thought I had at least a basic understanding of colorbox and have been able to use it with success for some time now. I am trying now to use the 'close' of one colorbox to redirect back to another colorbox, but when I do, that box will not close and instead just reopens itself. This is the code I use on the child colorbox to redirect back to parent colorbox:

    $.colorbox.close = function(){
        $.colorbox({ overlayClose:true, width:640, maxHeight:'100%', href:'[some url]', onComplete:function() {
                $.colorbox.resize();
            } });
        return;
    });

These are snippets I've used to try and get the parent box to close, none have worked. No matter what I use on the desired colorbox, it won't close. I've tried:

    $.colorbox.close = function(){
        $.colorbox.close();
    });

    $.colorbox.close = function(){
        $.fn.colorbox.close();
    });

    $('#cboxClose').click(function(){
        $.colorbox.close();
    });

Apparently I am not understanding why the first code worked and how to 'undo' the reset colorbox close function. Whatever I try, it either does no action or simply reopens itself. I'm beginning to think my idea is impossible to do. Help! Thanks in advance for any suggestions given.

cxanders commented 4 years ago

I found my mistake and have the right code now. This code is used in the child colorbox file and the parent doesn't need anything.

    var originalClose = $.colorbox.close;
    $.colorbox.close = function(){
       $.colorbox({ overlayClose:true, width:640, maxHeight:'100%', href:'[your parent url or whatever]', onComplete:function() {
               $.colorbox.resize();
                parent.$.colorbox.close = originalClose;
            } });
    };