最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open popup, click link, open in parent window, close popup? - Stack Overflow

programmeradmin1浏览0评论

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('.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:-(

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:-(

Share Improve this question edited Jan 31, 2013 at 5:28 Uttara 2,5343 gold badges25 silver badges35 bronze badges asked Jan 31, 2013 at 5:16 AdamAdam 511 gold badge1 silver badge2 bronze badges 1
  • 1 Note: Be sure to use a relative URL to your popup location. Otherwise, if the domains domain match (for example, mypage.com and www.mypage.com) you won't be able to access the parent window. – N Rohler Commented Jan 31, 2013 at 5:23
Add a comment  | 

2 Answers 2

Reset to default 8

I think you can use window.opener

window.opener.location.href=x

To access main opener window from a pop-up use the window.opener object. 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:

<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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论