javascript - window.top.postMessage 发送“null”作为它的 event.origin
问题描述
我正在尝试将消息从 iframe 发布到父窗口。我当然想origin
在处理之前检查消息。但似乎是"null"
。我究竟做错了什么?
// Parent:
$(document).ready(function() {
loadIframe();
iframeListen();
});
loadIframe = function() {
var iframe;
iframe = document.createElement('iframe');
iframe.src = "/load_pages.htm";
iframe.sandbox = "allow-scripts";
iframe.id = "page_iframe";
iframe.onload = function() {
return postToIframe();
};
return document.body.appendChild(iframe);
};
iframeListen = function() {
return window.addEventListener('message', function(event) {
return console.log(event.origin); // "null"
});
};
iframe 像这样发送它的消息:
// iframe:
<script>
window.top.postMessage('Hello parent', '*');
</script>
奖励信息:当我向 iframe 发送消息时,我得到了预期的来源:
// Parent:
$(document).ready(function() {
window.top.postMessage('Hello iframe', '*');
};
// iframe:
window.addEventListener('message', function(event) {
console.log(event.origin); // "http://localhost:3000"
});
解决方案
推荐阅读
- python - 如何将几条靠得很近的直线融合成一条线?
- python - ImportError:无法从“FlaskApp”(未知位置)导入名称“app”
- file - 无法使用 metasploit 框架(我使用的是 windows 8.1)
- android - 无法将 TextInputEditText 放置在 TextInput 布局的顶部。安卓工作室
- javascript - 无法使 epub 阅读器与来自 futurepress 的 Epub.js 一起显示
- c# - 如何比较两个 IEnumerable<> 对象并返回一个新对象?
- reactjs - 在模块内使用 FormattedMessage 时出错:错误:[React Intl] 找不到所需的 `intl` 对象
- java - 无法从 Java servlet 中的请求对象中读取 JSON 正文
- flutter - 刷新应用程序时,ChangeNotifierProxy Provider 和 ChangeNotifier Provider 失去价值
- flutter - 我正在尝试将 object.parameter 传递给自定义小部件并失败