javascript - Edge 中的 window.postMessage 时无法访问源对象
问题描述
我目前正在使用Edge 44.18362
,我有一个身份验证窗口和一个打开它的父窗口,一旦身份验证完成,我将消息发布到父级(开启器窗口),如下所示:
$window.opener.postMessage(data,URL);
我正在像这样在父窗口中收听事件:
window.addEventListener('message', someFunc, false);
并且someFunc()
定义如下:
function someFunc(windowData){
//here I am accessing source as windowData.source
}
我正在尝试访问source
属性MessageEvent
以访问 URL 和内容。在 Chrome/Firefox 中,我能够访问source
对象和功能工作正常,但是当涉及到 Edge 时,我无法访问source
对象并且我在控制台中看到此错误。
这里出了什么问题,可以做些什么来解决这个问题?
!0:
解决方案
我使用以下代码示例进行了测试,它可以在 Edge 中正常工作:
页1.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script>
function openChild() {
var childwin = window.open('Page2.html', 'popup', 'height=300px, width=500px');
}
window.addEventListener("message", (event) => {
let txt = document.querySelector('#txtMsg');
txt.value = `Name is ${event.data.pName} Age is ${event.data.pAge}`;
console.log(event.source); //update: get event.source
});
</script>
</head>
<body>
<form>
<fieldset>
<input type='button' id='btnopen' value='Open child' onclick='openChild();' />
<input type='text' id='txtMsg' />
</fieldset>
</form>
</body>
</html>
页面2.html:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script>
function sendMessage() {
let msg = { pName: "Bob", pAge: "35" };
window.opener.postMessage(msg, 'http://domain_name/Page1.html')
window.opener.focus();
}
</script>
</head>
<body>
<form>
<fieldset>
<input type='button' id='btnSendMsg' value='Send Message' onclick='sendMessage();' />
</fieldset>
</form>
</body>
</html>
结果:
您可以参考示例,如果仍然无法正常工作,请提供可以重现问题的最小示例。仅使用您提供的上述代码,我无法重现该问题。
推荐阅读
- html - JSP 页面不渲染图像,但状态为 200
- swift - swift中的运算符或方法,用作python的海象运算符
- sql - 如何在 phpmyadmin 中导入这个 github repo 的 sql 文件
- java - Onhandleintent 昨天工作,现在没有
- content-management-system - Magento 2.3.3:如何在 CMS 页面中获取 form_key
- r - 删除包含零的单元格但保留该行
- apache-spark - 如何以逗号分隔的形式显示火花流作业的数据帧输出?
- javascript - 如何在 TypeScript 中将 PFX 证书转换为 PEM 证书(通过使用 openssl 或任何其他包)?
- python - Python - Spotify API 返回错误 400“格式错误的 JSON”
- pandas - 将平均行添加到包含“-”的数据帧