FancyBox iframe returns parent.$ as undefined (usi

2019-01-12 09:08发布

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

 <script type='text/javascript' 
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'>
 </script>
 <script type="text/javascript">
 jQuery(document).ready(function($){
     (function($) {
         $.fn.closeFancyBox = function() {
             $(this).click(function() {
                 parent.$.fancybox.close();
             });
         };
      })(jQuery);
      $('#cancel').closeFancyBox();
      });
 });
 </script>

Replacing parent.$.fancybox.close(); with alert('clicked'); works just fine. I don't understand why parent.$ is undefined when the iframe is in the same domain.

I'm using WordPress 2.9.1, with the FancyBox for Wordpress plugin.

  • main page: //server.local/web/test/index.php
  • iframe page: //server.local/web/test/wp-content/plugins/wp-test/test.htm

The first of these urls is the main page, the second is the iframe page; server.local is my home test server.

Any ideas? I can pastebin the entire source if it would be helpful.

9条回答
迷人小祖宗
2楼-- · 2019-01-12 09:36

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-12 09:38

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!!

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-12 09:38

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

type:'iframe'

When calling the fancybox, it solved my problem

查看更多
贪生不怕死
5楼-- · 2019-01-12 09:39

I had to do this:

window.top.window.$.fancybox.close();

Got type error before.

查看更多
时光不老,我们不散
6楼-- · 2019-01-12 09:42

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.

查看更多
beautiful°
7楼-- · 2019-01-12 09:42

HI, Anyone who is having trouble closing a Fancy Box iFrame using a manual install of Fancy Box in Wordpress 3.0:

Use this link in your iframe:

<a href="#" onClick="parent.jQuery.fancybox.close();" title="Close window">close fancybox</a>

It works :)

查看更多
登录 后发表回答