7

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
}); 
Dharman
  • 30,962
  • 25
  • 85
  • 135
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130

2 Answers2

5

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.

Andy
  • 414
  • 4
  • 8
  • works well. In case anyone is using Fancybox 2, it changed the function from `onClosed` to `afterClose`. – Raptor Mar 10 '16 at 10:30
2

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.

Benbob
  • 13,876
  • 18
  • 79
  • 114
  • I understand what you have put but that is simply letting me do something or calling a function onclose/cleanup/open... it's not actualy getting a varibal from the child window. And using it within the parent. Im looking to pass a variable back from the child. – Phil Jackson Jan 04 '11 at 06:01
  • Not sure what the jQuery equivalent is but you can use .contentWindow to access the iframe. See my edit above. – Benbob Jan 04 '11 at 06:14
  • 1
    Hi thanks after the first edit did not work I skipped calling the function altogether and accesed the DOM for my var cheers (my solution is in my orig question). – Phil Jackson Jan 04 '11 at 06:37
  • I thought you were trying to access a javascript variable. A little context (html source) would have helped. Glad you got it sorted out. – Benbob Jan 04 '11 at 07:09
  • I had the same problem and the `fancybox-frame` selector was turning up nothing. Here's what worked for me: http://stackoverflow.com/questions/726816/how-to-write-this-in-jquery-window-parent-document-getelementbyidparentprice – Keyslinger Oct 20 '11 at 09:41
  • in case anyone is using Fancybox 2, it changed the function from `onClosed` to `afterClose`. – Raptor Mar 10 '16 at 10:23