关闭Fancybox 2窗口并重定向父窗口

关闭Fancybox 2窗口并重定向父窗口

问题描述:

我有一个Fancybox 2 iframe模式窗口,其中包含添加到购物车"表单.提交表单后,将在Fancybox模式窗口中加载成功/确认页面.

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.

我想在此成功/确认页面上运行一个脚本,该脚本关闭Fancybox窗口并将父窗口重定向到以下URL:'/shop/basket'我该怎么做?

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?

这是我目前获得的代码(它仅关闭Fancybox窗口,但不执行父窗口重定向):

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>

这是我如何在我的functions.js文件中初始化Fancybox的方法:

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

这段代码可以满足我的需求-但是有人可以想到实现此目的的更好方法吗?这只是我一起破解的东西:

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>

不要尝试从子页面进行重定向,而是尝试从父页面本身进行重定向.

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

如果您的父页面(functions.js)中包含以下代码:

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

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

然后只需向其中添加afterClose回调;

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

...您仍然需要在子页面上执行parent.$.fancybox.close();

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