Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fancybox - passing variable back to parent when fancybox window is closed

I was wondering if it is possible to pass back a variable from fancybox child to parent once child is closed?

function cleanUp(){
    var bla = $("#fancybox-frame").contents().find('input[name=pd_id]');
    alert(bla.val());
}
$("#tree .product a[class!=copy]").fancybox({
    'width'             : 770,
    'height'            : '95%',
    'autoScale'         : false,
    'transitionIn'      : 'none',
    'transitionOut'     : 'none',
    'type'              : 'iframe',
    'onCleanup'         : cleanUp
}); 
like image 546
Phil Jackson Avatar asked Jan 04 '11 05:01

Phil Jackson


2 Answers

In the parent, I define

var $_returnvalue = false;

And onClosed looks like this:

onClosed: function() {
    if ($_returnvalue != false)
    {
        // Do something in the parent
    }

And in the fancybox page, when something is ready to be returned, I set that value to parent.$_returnvalue and then close the box. This works well for selecting something from a list and passing that value back to the calling page.

like image 175
Andy Avatar answered Nov 11 '22 10:11

Andy


You can see in the fancybox api : onClosed... Will be called once FancyBox is closed

$('.overlay').fancybox({
    onClosed: function() { 
        var bla = $('#targetFrame')[0].contentWindow.targetFunction(); 
    }
});

There is no child window/iframe, the box is simply an absolutely positioned div. It's in the same DOM as the page.

like image 36
Keyo Avatar answered Nov 11 '22 08:11

Keyo