Javascript 打开弹出窗口,单击链接,在父窗口中打开,关闭弹出窗口?

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

Open popup, click link, open in parent window, close popup?

javascriptpopup

提问by Adam

I need help please. What I want to do from my page is to open a popup for which I use this:

我需要帮助。我想从我的页面做的是打开一个弹出窗口,我使用它:

<a class="txt-button" onclick="javascript:void window.open('http://mypage.com/1/index.html','','width=700,height=500,resizable=false,left=0,top=0');return false;">Buy Now</a>

When a link on the popup is clicked I want it to open in the main window and for the popup to close.

单击弹出窗口上的链接时,我希望它在主窗口中打开并关闭弹出窗口。

I've tried many things and cant get it to work:-(

我已经尝试了很多东西,但无法让它工作:-(

回答by Arun P Johny

I think you can use window.opener

我想你可以使用window.opener

window.opener.location.href=x

回答by TildalWave

To access main openerwindow from a pop-upuse the window.openerobject. You can access any opener property, or even a function call like that (as long as domains match, as @N Rohler pointed out in the comment to your question), for example to navigate use window.opener.location.href. Then you close your pop-up with window.close();like this:

要从弹出opener窗口访问主窗口,请使用该window.opener对象。您可以访问任何 opener 属性,甚至是这样的函数调用(只要域匹配,正如@N Rohler 在对您的问题的评论中指出的那样),例如导航 use window.opener.location.href。然后你window.close();像这样关闭你的弹出窗口:

<a href="JavaScript:void(0);" onclick="openInParent('http://example.com/');">
  click me
</a>

<script>
  function openInParent(url) {
    window.opener.location.href = url;
    window.close();
  }
</script>