32

I've got a popup window opened using window.open(). What I want now is for a user to be able to click one of 2 links within this new window: "Allow" or "Don't Allow".

When a user clicks one of those links the 'popup' window should close, and return either "allow" or "don't allow" or something along those lines, true/false would do, to the parent window.

Is it possible? If so, how?

Code:

var authWindow = window.open('auth.php', 'authWindow', 'options...');

Then just 2 anchors inside auth.php?

Sharon
  • 919
  • 7
  • 7
Joey Emery
  • 684
  • 2
  • 6
  • 15
  • did you tried something on your own so far ? You could use modal popups – mas-designs Feb 14 '12 at 11:34
  • Code posted? Although I don't really see the need, haha. – Joey Emery Feb 14 '12 at 11:42
  • What are you going to be doing with these values afterwards? – Anthony Grist Feb 14 '12 at 11:45
  • 1
    personally i try to avoid window popups. Normally I use jquery-ui dialogs (or flowtools overlay, or wijmo dialogs) to simulate pop-up behavior. IMO it looks nicer, is cleaner and avoids problems with browsers blocking popups - just a small remark :) – giorgio Feb 14 '12 at 11:54

2 Answers2

69

In the calling (parent) window add such JS code:

function HandlePopupResult(result) {
    alert("result of popup is: " + result);
}

In the child window code add this:

function CloseMySelf(sender) {
    try {
        window.opener.HandlePopupResult(sender.getAttribute("result"));
    }
    catch (err) {}
    window.close();
    return false;
}

And have such links to close the popup:

<a href="#" result="allow" onclick="return CloseMySelf(this);">Allow</a>
<a href="#" result="disallow" onclick="return CloseMySelf(this);">Don't Allow</a>
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • function CloseMySelf is causing an error because the link is just going straight to # and the window isn't closing. – Joey Emery Feb 14 '12 at 12:04
  • @Joey sorry, had bug in my code.. see now. (in JavaScript, `catch` must have following brackets) – Shadow The GPT Wizard Feb 14 '12 at 12:10
  • Curious: is there a reason you wouldn't just pass 'allow' or 'disallow' as a parameter to the CloseMySelf() function? What is the advantage to use the result attribute design? – scubasteve May 01 '15 at 06:08
  • 2
    @SteveK better separation between logic and code, since the "allow" and "disallow" are part of the element itself, as I see it at least. Also gives better flexibility, e.g. if we want to use more arguments in the future, just read them from the element itself. – Shadow The GPT Wizard May 01 '15 at 06:13
  • This is just going to work if parent and child pages are in same server, rigth? – Daniel Delgado Mar 21 '18 at 16:40
  • @Daniel same domain, yes. See also [this](https://stackoverflow.com/questions/18625733/how-do-i-get-around-window-opener-cross-domain-security). – Shadow The GPT Wizard Mar 21 '18 at 19:31
4

you can use window.opener for this.

Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86