Fancybox afterClose event not working

Thempower picture Thempower · Sep 17, 2014 · Viewed 13.9k times · Source

I'm having a problem using the fancybox API after Close.

I open the function when people click on this:

<a class="fancybox fancybox.iframe btn" href="myurl.php"></a>

The javascript behind this is:

$('.fancybox.iframe')
    .fancybox({
    arrows: false,
    padding: 0,
    overlay: {
        locked: false
    },
    beforeClose: function () {
        location.reload();
    }
});

And it never reload the page when I close it. Can somebody help me? Thank you !

Answer

JFK picture JFK · Sep 17, 2014

Some clarification to avoid further confusion:

Based on your html

<a class="fancybox fancybox.iframe btn" href="myurl.php"></a>
  1. Use the afterClose callback (not an event) instead of beforeClose. For further reference check Tips & Tricks => No.11

  2. The class fancybox is used to bind the selector to fancybox so your initialization code should look like this

    jQuery(document).ready(function ($) {
        $('.fancybox').fancybox({
            arrows: false,
            padding: 0,
            helpers: {
                overlay: {
                    locked: false
                }
            },
            afterClose: function () {
                location.reload();
            }
        });
    }); // ready
    
  3. The (valid) fancybox.iframe class tells fancybox the type of content it should handle, but you don't use it to bind the selector to fancybox.

See JSFIDDLE

NOTE : this is for fancybox v2.x