Hyperlink Changing Parent Window and Closing Popup


How can I do this so that both happen at the same time?

Any help is greatly appreciated,

P.S. I’m rather new to JavaScript, so please provide as much code and documentation as possible. Thanks!

Quite simple; just call the closePopup() function on unload.
If you want the popup closed when clicking on a specific link, call closePopup() on the onClick event for the link instead.
(The call to closePopup() in openPopup() is just an extra thing, to avoid more than 1 popup window opened by this page)

    <script language="JavaScript">
      var w = null;
      function openPopup()
        w = window.open('', '', 'width=200,height=200');
        w.document.write('Hello, I just popped up');
      function closePopup()
        if(w != null)
  <body onUnload="closePopup();">
    <a href="javascript:void(0);" onclick="openPopup();">Open popup</a>
    <a href="anotherpage.htm">A link to another page</a>

OK, read your question one more time and realized that you wanted to change the parent window’s location and then close the popup - from the popup window.
Here’s an updated openPopup() function and a new change() function (same html and closePopup() code as in the previous post)

      function openPopup()
        w = window.open('', '', 'width=200,height=200');
        var s = 'Hello, I just popped up<br>';
        s += '<a href="javascript:void(0);" onClick="window.opener.change();">';
        s += 'Change parent and close me</a>'

      function change()
        window.location.href = 'anotherpage.htm';


Thanks for your help. However, I’m very confused.

I have a popup window that opens when somebody visits my website. There is a form in this winodw where somebody enters their email address. Then a “thank you” page comes up. In the “thank you” page, it says “If you have any questions, please don’t hesitate to contact us.” The contact us is linked to /contactus.htm. When somebody clicks on “contact us,” I want the popup to close and then the parent window to change to contactus.html. How can I do this?

You may have already answered this, but I’m confused.


I want the popup to close and then the parent window to change to contactus.html. How can I do this?

In the popup, add this link:
<a href=“javascript:void(0);” onClick=“window.opener.location.href=‘contactus.html’;”>Contact us</a>

And, of course, if you don’t have the
<body onUnload=“closePopup();”>
in the main page, you must add
window.close; to the link in the popup:
<a href=“java script:void(0);” onClick=“window.opener.location.href=‘contactus.html’;window.close();”>Contact us</a>


Thank you very much! :slight_smile:


How about a twist on this:

I have a page with a link that loads another page. No problem, straight html. However the loaded page takes some time to load since it’s parsing some PHP. So I created a little javascript that opens a new popup window that says “your page is being loade, chill out” or something. I called this popup window “popup” from the javascript that opens it:

onClick="window.open(‘url’, ‘popup’, width, height)

Now what I want to do is close the popup window once the called page loads. So I thought that if I put into the called page:

<body onLoad="window.close(‘popup’)>

it would do the trick, but all it does is close itself! How can I tell it to close the popup window?