如何从 https 弹出窗口重新加载 http window.opener?

发布于 2024-09-13 08:22:08 字数 190 浏览 6 评论 0原文

我有一个 http 窗口,它会打开一个安全弹出窗口以向第三方网站提交表单。弹出窗口关闭后,我想重新加载打开器,以便它反映表单提交的结果。

由于开启器和弹出窗口使用不同的协议(http 和 https),因此我无法以简单的方式(window.opener.location.reload())执行此操作。还有别的办法吗?我必须冒险尝试 JSONP 吗?

I have an http window which opens a secure popup to submit a form to a third party web site. After the popup is closed, I would like to reload the opener so that it reflects the results of the form submission.

Since the opener and the popup use different protocols (http and https), I can't do it in the straightforward way (window.opener.location.reload()). Is there another way? Do I have to venture into JSONP?

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

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

发布评论

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

评论(2

丶情人眼里出诗心の 2024-09-20 08:22:08

笨拙的方法:设置超时来检查打开的弹出窗口是否已关闭。

function checkClosed(){
    if(new_win.closed){
        alert('Window was closed');
    }
    else{
       window.setTimeout(checkClosed,1000);
    }
} 

new_win = window.open('about:blank');
window.setTimeout(checkClosed,1000);

Kludgy way: set timeout to check if opened popup was closed.

function checkClosed(){
    if(new_win.closed){
        alert('Window was closed');
    }
    else{
       window.setTimeout(checkClosed,1000);
    }
} 

new_win = window.open('about:blank');
window.setTimeout(checkClosed,1000);
套路撩心 2024-09-20 08:22:08

我知道这是一个超级老的问题,但我找到了一个更好的方法来做到这一点: 跨文档消息传递

$('a#popup').on('click', function(event) {
    event.preventDefault();
    $this = $(this);
    authenticationWindow = window.open($this.href, 'authenticationPopup');
});

window.addEventListener('message', function (evt) {
    // simplified, should check for matching event.origin
    if (event.data == 'OK' && event.origin == window.location.protocol+'//'+window.location.host) {
        authenticationWindow.close();
    }
});

在弹出窗口中(当然,如果您控制它,至少在最后一页),您需要这样做:

opener.postMessage('OK', window.location.protocol+'//'+window.location.host);

这将关闭弹出窗口,并且还允许您执行其他操作,例如通过 Ajax 更新数据或重新加载页面。

I know that this is a super old question, but I found a better way to do this: Cross-document messaging:

$('a#popup').on('click', function(event) {
    event.preventDefault();
    $this = $(this);
    authenticationWindow = window.open($this.href, 'authenticationPopup');
});

window.addEventListener('message', function (evt) {
    // simplified, should check for matching event.origin
    if (event.data == 'OK' && event.origin == window.location.protocol+'//'+window.location.host) {
        authenticationWindow.close();
    }
});

While from the pop up (of course if you control it, at least the last page) you need this:

opener.postMessage('OK', window.location.protocol+'//'+window.location.host);

This will close the popup, and allows you to do other stuff as well, like update data via Ajax or reload the page.

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