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

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

查看更多
Rolldiameter
3楼-- · 2019-01-12 09:55

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.

查看更多
Emotional °昔
4楼-- · 2019-01-12 09:55

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');
查看更多
登录 后发表回答