FancyBox iframe returns parent.$ as undefined (using WordPress)

前端 未结 9 2402
无人共我
无人共我 2020-12-04 01:58

I\'m trying to close FancyBox from within the iframe, but parent.$ is always undefined. This is my iframe JavaScript:

 

        
相关标签:
9条回答
  • 2020-12-04 02:13

    Any variations of parent.fancybox.close() didn't work for me, must be some lib conflict.

    here is my working workaround for latest fancybox, you should use css display property instead of .hide() method as in that case fancybox will not open again.

    parent.jQuery('#fancybox-overlay').css('display', 'none');
    parent.jQuery('#fancybox-wrap').css('display', 'none');
    
    0 讨论(0)
  • 2020-12-04 02:13

    This works for me ;)

    <a href="javascript:parent.jQuery.fn.fancybox.close();" >
    

    Thanks for this post, it actually guided me a little... I feel bad because it took me a only a few minutes and I KNOW how frustrating Fancybox for Wordpress is!!

    0 讨论(0)
  • 2020-12-04 02:13

    None of the suggestions worked for me. I had to work around it using the following code. The latest version may be supporting the parent.jQuery.fancybox.close(); approach, but the older versions do not work with that.

    For existing sites with older versions of the plugins/Jquery, try this

    function close_window()
    {
     $("#fancy_outer",window.parent.document).hide();
     $("#fancy_overlay",window.parent.document).hide();
     //window.top.window.$.fancybox.close(); this also does not work :(
    }
    

    you could declare and use the function close_window within the Iframe content.

    0 讨论(0)
  • 2020-12-04 02:14

    I had the same problem and noticed that I am not using the

    type:'iframe'
    

    When calling the fancybox, it solved my problem

    0 讨论(0)
  • 2020-12-04 02:18

    Spent quite a few hours trying to debug this and got nowhere. So I switched out the 'FancyBox for WordPress' plugin with the latest version of FancyBox, and it was fixed. Really should have tried that earlier.

    After having spent some time with WordPress and its various plugins, I'd recommend calling things manually rather relying on plugins. It just adds another layer of complexity that, if you know what you're doing, doesn't need to be there.

    Thanks to Doug for pointing out the appropriate syntax for iframe to parent window jQuery in WordPress.

    0 讨论(0)
  • 2020-12-04 02:24

    It is undefined because WordPress runs jQuery in noConflict mode. Use this instead:

    parent.jQuery.fancybox.close();
    

    noConflict mode means $ does not equal jQuery. You have to explicitly use jQuery to access what you normally can access with $.

    0 讨论(0)
提交回复
热议问题