【问题标题】:Addin can not receive postMessage anymore after reloading the popup重新加载弹出窗口后,插件无法再接收 postMessage
【发布时间】:2017-05-10 15:36:01
【问题描述】:

我正在通过Excel JavaScript API 制作 Excel 加载项。这个插件有一个按钮,点击它会启动popup = window.open("https://localhost:3000/#/new/", "popup", "width=1000, height=1100")并弹出一个浏览器窗口。

在弹出窗口中,我可以使用以下代码向插件发送消息:

if ($window.opener !== null) {
    alert($window.opener.location.href); // https://localhost/App/Home.html?et=
    $window.opener.postMessage(msg, $window.opener.location.href);      
}

在插件中,我可以使用以下代码从弹出窗口接收消息:

function receiveMessage(event) {
    console.log("RECEIVED: " + JSON.stringify(event.origin));
    if (event.origin === ...) {
        action(event.data);
    }
};

所以效果很好:当插件收到消息时,它显示RECEIVED: "https://localhost:3000"

但是,我意识到如果我通过cmd+r 刷新弹出窗口,弹出窗口仍然可以发送消息($window.opener.location.href 仍然相同),但插件无法再接收它; RECEIVED: "https://localhost:3000" 不再显示。

有谁知道发生了什么以及如何解决它?

【问题讨论】:

    标签: cross-domain office-js page-refresh postmessage window.opener


    【解决方案1】:

    您能否尝试使用 Office 加载项对话框 API 而不是 window.open?它专为与加载项一起使用而设计。有关概述,请参阅:Use the Dialog API

    【讨论】:

    • 谢谢...我将尝试下一个版本的 Dialog API。对于这个版本,我想用 window.open 做到最好...
    猜你喜欢
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2011-05-11
    • 2014-09-20
    • 2012-12-19
    相关资源
    最近更新 更多