Close Fancybox 2 window and redirect parent window

霸气de小男生 提交于 2019-12-13 06:31:06

问题


I've got a Fancybox 2 iframe modal window that contains an 'Add to Cart' form. When the form is submitted, a success/confirmation page will be loaded within the Fancybox modal window.

I want to run a script on this success/confirmation page that closes the Fancybox window and redirects the parent window to this URL: '/shop/basket' How can I do this?

Here's the code I've got at the moment (which just closes the Fancybox window, but doesn't do the parent window redirect):

<script type="text/javascript">
  $(function() { parent.$.fancybox.close(); });
</script>

Edit:

This is how I initialise the Fancybox within my functions.js file:

$(".add-to-cart-popup").fancybox({
    width       : 580,
    height      : 744,
    closeClick  : false,
    scrolling   : 'auto'
});

Edit 2:

This code does what I'm looking for - but can anyone think of any better ways of doing this? This is just something I've hacked together:

<script type="text/javascript">
  $(function() { parent.$.fancybox.close(); });
  window.parent.location.href = '/shop/basket';
</script>

回答1:


Don't try the redirect from the child page, but from the parent page itself.

If you have this code in your parent page (functions.js) :

$(".add-to-cart-popup").fancybox({
    width       : 580,
    height      : 744,
    closeClick  : false,
    scrolling   : 'auto'
});

Then just add the afterClose callback to it like ;

$(".add-to-cart-popup").fancybox({
    width       : 580,
    height      : 744,
    closeClick  : false,
    scrolling   : 'auto',
    afterClose  : function() {
       location.href = "/shop/basket";
    }
});

... you still need to perform parent.$.fancybox.close(); from the child page



来源:https://stackoverflow.com/questions/13846660/close-fancybox-2-window-and-redirect-parent-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!