javascript 使用javascript返回父窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8032832/
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-10-26 02:02:56  来源:igfitidea点击:

return back to parent window using javascript

javascript

提问by pranay godha

I am popping out a child window from parent window.Now, in child window, I have a html form that a user will fill and submit and request will go on server site.Now, I wanted response to this request redirected to parent window and my child window automatically closed out.Hope I am clear enough. If not let me know. thank you in advance.

我正在从父窗口弹出一个子窗口。现在,在子窗口中,我有一个 html 表单,用户将填写并提交该表单,请求将在服务器站点上进行。现在,我想将此请求的响应重定向到父窗口和我的子窗口自动关闭。希望我足够清楚。如果不让我知道。先感谢您。

采纳答案by Meligy

Check jQuery Popup Window Return Value to Parent

检查jQuery 弹出窗口返回值给父级

basically the logic can be:

基本上逻辑可以是:

  1. You response returns a page that contains a hidden-div that contains the message you want to display (hidden with CSS set display:none;)

  2. That child page returned also contains some JavaScript that fills in the parent window with the DIV content, parent/* or opener */.document.getElementById(...).innerHTML = ...;
    or calls a function on the parent to tell it it's now complete, like window.opener/*or parent*/.formIsComplete();where formIsComplete()is a JavaScript function in the parent document/window.

  3. The last line of the JavaScript on the child window, will close itself, as simple as window.close();.

  1. 您的响应返回一个页面,其中包含一个 hidden-div,其中包含您要显示的消息(使用 CSS set 隐藏display:none;

  2. 返回的子页面还包含一些用 DIV 内容填充父窗口的 JavaScript,parent/* or opener */.document.getElementById(...).innerHTML = ...;
    或者调用父窗口上的函数来告诉它现在已经完成,就像父文档/窗口中的 JavaScript 函数在window.opener/*or parent*/.formIsComplete();哪里formIsComplete()

  3. 子窗口上 JavaScript 的最后一行将自行关闭,就像window.close();.

回答by Thai

If the pop-up window is created using window.open, then the pop-up window can access the opening window using window.opener.

如果使用 来创建弹出窗口window.open,则弹出窗口可以使用 访问打开的窗口window.opener

On the main window, you have to create a function to receive data from the popup, something like this:

在主窗口中,您必须创建一个函数来从弹出窗口接收数据,如下所示:

window.receiveDataFromPopup = function(data) {
    // do things with data.
};

Then in the pop-up window, you can call that function on the opener like this:

然后在弹出窗口中,您可以像这样在 opener 上调用该函数:

window.opener.receiveDataFromPopup(data);

To close the window, just use

要关闭窗口,只需使用

window.close();

回答by Saeros

There is a way to access the parent window from the child. It is

有一种方法可以从子窗口访问父窗口。它是

window.opener //use window.opener to access the parent window.

So in the child window, once your form submission completes, just call

所以在子窗口中,一旦您的表单提交完成,只需调用

window.opener.yourFunc() //Your func is a function on parent. Call this from the child.

Once this function is called close your child window using

一旦调用此函数,请使用以下命令关闭您的子窗口

window.close();

回答by Zeke

<script type='text/javascript'>
  window.opener.location.reload();    
  window.close();
</script>