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

发布于 2025-01-05 12:27:24 字数 373 浏览 3 评论 0原文

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

当用户单击其中一个链接时,“弹出”窗口应关闭,并返回“允许”或“不允许”或类似的内容,true/false 就可以了,到父窗口。

是否可以?如果是这样,怎么办?

代码:

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

那么 auth.php 内只有 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".

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2025-01-12 12:27:25

您可以使用 window.opener 来实现此目的。

you can use window.opener for this.

盛装女皇 2025-01-12 12:27:24

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

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;
}

并有这样的链接来关闭弹出窗口:

<a href="#" result="allow" onclick="return CloseMySelf(this);">Allow</a>
<a href="#" result="disallow" onclick="return CloseMySelf(this);">Don't Allow</a>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文