Popup window to return data to parent on close

你说的曾经没有我的故事 提交于 2019-11-26 06:35:48

问题


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?


回答1:


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>



回答2:


you can use window.opener for this.



来源:https://stackoverflow.com/questions/9276086/popup-window-to-return-data-to-parent-on-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!