I have a main document open in a browser and I created a pop-up window (with a link in it) to display when I loaded the browser. I want to click the link in the pop-up window and have it display the link's URL in the existing browser window. I also want to close the pop-up window upon clicking the link.

So far this is what I have:

<html>
   <title>Subscription</title>

    <link href="cover.css" rel="stylesheet" type="text/css" />


    <!-- this function displays the contents of the form.htm file in the main browser window (cwj.htm). -->

    <script type="text/javascript">
        function showForm() {
            opener = window.open("form.htm", "form", "");
            opener.location.href = "cwj.htm"; 
            opener.close(); 
        }

    </script>
    </head>

    <body>

    <div id="main">
    <p><a href="form.htm"
        onclick="return(showForm('form.htm'))">
       <b>Order 12 issues <br />for: $9.95*</b>
       </a>
    </p>
    <p><img src="cover.jpg" alt="The Civil War Journal" /><br />
       *25% off the <br />newsstand price!</p>
    </div>

    </body>
    </html>

With the existing code, the pop-up window opens upon loading and displays a link. When I click on the link the URL opens in the pop-up window (not in the main window) and the pop-up window does not close (I need it to close).

How can I get it to do what I want it to? Any help is greatly appreciated. Thanks!

3 answers

Ashley 5
1
point
This was chosen as the best answer

Thanks for your help guys. I finally got it to work. Here is my code:

<script type="text/javascript">
    function showForm() {
        window.opener.location="form.htm";
        self.close();
    }

</script>
</head>

<body>

<div id="main">

<p><a href="javascript:void(null)" onClick="showForm()">

   <b>Order 12 issues <br />for: $9.95*</b>
   </a>
</p>
Answered almost 5 years ago by Ashley
  • @Ashley, if the opener window is closed (say by accident), the code will not work. Rob's method has a fallback in case that happens. o.k.w almost 5 years ago
3
points

Put the code in the form.htm file instead of in your page. So leave that line above in to open the window, but in your form.htm make the link something like this (untested):

<a href="cwj.html" onclick="window.opener.location.href=this.href;window.close()">Click here</a>

Also, opener is already available from the window object, so redefining it in your code could lead to confusion.

Answered almost 5 years ago by Rob Crowther
  • I need to keep the code in the cover.htm file. I don't quite understand your answer can you explain it again? I tried putting in the suggested link but it didn't work. It made the pop-up window load a page that was not viewable. Ashley almost 5 years ago
  • @Ashley, in your question, there are 2 pages, form.htm and cwj.htm. Rob and myself assumed form.htm is the pop-up, cwj.htm is the main page. So now you have another cover.htm? Care to elaborate? o.k.w almost 5 years ago
  • @Ashley, ok ok, i got it. 'cover.htm' is the main page while 'cwj.htm' is the link on 'form.htm' that will close the pop-up upon clicking. What was I thinking? :P o.k.w almost 5 years ago
o.k.w 2355
3
points

Rob is spot on. I would like to suggest you do this for the main page. Drop the function 'showform', construct the anchor to the following:

<a href="form.htm"
    target="form" onclick="window.open(this.href, 'form');">
   <b>Order 12 issues <br />for: $9.95*</b>
   </a>

The target="form" attribute will allow pop-up of 'form.htm' for those users with disabled javascript.

Answered almost 5 years ago by o.k.w