HWL1223
HWL1223

Reputation: 21

Need to close an open pop up and navigate to an URL

I am new at scripting and am lost.

I have a page that calls a popup in two places by this link:

Click <a href=""


onclick="window.open('poptest.php','chat_popup','toolbar=0,menubar=0,scrollbars=1,width=400,height=400'); return false;">here</a> for the pop-up Chat window only.

Also on the page is a link to this window.location.href command:

<button onclick="goto()">Back to the Intro Page</button>
<script>

    function goto() {
        window.location.href="http://www.streamcasters.com/temchat/sidebyside.html";
    }
    </script>

Users are not closing the popup before using the window.location.href command.Is there a dual-command where when a user clicks the location script it ALSO closes the specific onclick=window.open tab (the popup that might be left open when they go to a page where the formatting is different)?

Thank you

Upvotes: 0

Views: 1142

Answers (1)

Manngo
Manngo

Reputation: 16413

You will need to store the opened window in a variable, and use that variable to close the window when the time comes

  • Create a global variable, say popup. Normally, global variables are a sign of poor design, but, in this case, you will need to access the same variable from more than one place.
  • Rather than go straight into the window.open, take advantage of the fact that the method contains a return result. So, popup = window.open(…).
  • Use the beforeunload event to close the window

Something like this:

var popup;
popup = window.open(…);
window.onbeforeunload = function() {
    if(popup) popup.close();
};

Upvotes: 1

Related Questions