Javascript 关闭子窗口,重定向父窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5656349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:18:07  来源:igfitidea点击:

Close child window, redirect parent window

javascriptredirectwindow

提问by bear

I've got a page which opens a child window, from a parent.

我有一个页面可以打开一个来自父级的子窗口。

After the user has done something in the child window, I need the child window to close, and its parent window to be redirected to a page, on the click of a button in the child window.

用户在子窗口中完成某些操作后,我需要在子窗口中单击按钮时关闭子窗口,并将其父窗口重定向到一个页面。

Any ideas?

有任何想法吗?

回答by Mark Kahn

from child:

从孩子:

opener.location.href = '/redirect';
close();

回答by Marc Abramowitz

The key is to use window.openerto access the parent window:

关键是用来window.opener访问父窗口:

<script>
    window.opener.location = '/redirect.html';
    window.close();
</script>

回答by Naftali aka Neal

try this:

尝试这个:

var parent = window.opener;

parent.location ='some url';

window.close();

here is an example in a fiddle: http://jsfiddle.net/maniator/jGbZq/

这是小提琴中的一个例子:http: //jsfiddle.net/maniator/jGbZq/

回答by Gaurav123

Please try below :

请尝试以下:

window.opener.location.reload();
close();
  1. window.opener.location.reload(); will reload the parent window.
  2. close() will close the child window
  1. window.opener.location.reload(); 将重新加载父窗口。
  2. close() 将关闭子窗口

It works fine for me.

这对我来说可以。