弹出窗口以在关闭时将数据返回给父级

2022-01-20 00:00:00 popup jquery javascript

我使用 window.open() 打开了一个弹出窗口.我现在想要的是让用户能够点击这个新窗口中的 2 个链接之一:允许"或不允许".

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

当用户单击其中一个链接时,弹出"窗口应该关闭,并返回允许"或不允许"或类似的内容,true/false 会做,到父窗口.

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?

代码:

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

那么auth.php里面只有2个锚点?

Then just 2 anchors inside auth.php?

推荐答案

在调用(父)窗口中添加这样的JS代码:

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

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

在子窗口代码中添加:

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>

相关文章