English 中文(简体)
FancyBox iframe returns parent.$ as undefined (using WordPress)
原标题:

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.

最佳回答

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.

问题回答

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 $.

My answer is not related to wordpress, but one for fancybox in general.

in the iframe, if you have included the main jquery script(jquery-1.5.2.min.js), then it will conflict with the one on the main page, and parent.$.fancybox will not work in this case.

other jquery related stuff (like for eg: the tabs) will work inside the iframe. hence, it wont occur to a novice programmer that the second jquery script inside the iframe is the villain.

I had to do this:

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

Got type error before.

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 );

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

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.

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

type: iframe 

When calling the fancybox, it solved my problem

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 :)





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签