【发布时间】: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