【发布时间】:2020-03-29 05:48:46
【问题描述】:
我正在尝试将消息从父窗口发布到它打开的子窗口。但是,该消息未发布。
在父窗口脚本中:
function editAnnotation(annotKey){
var annotString = annotToString();
//open up the child window addAnnot.html.
var editAnnotWindow = window.open("editAnnot.html", "Ratting","width=200,height=400,0,status=0,scrollbars=1");
//send a message containing all the info from the current field. This message will cause all the fields to be prepopulated w info from annotation
editAnnotWindow.postMessage(annotString, '*');
}
在子窗口脚本中:
window.onload = addListeners();
/***********************************************************************
*
* Function that adds listeners for messages
*
*/
function addListeners() {
console.log("addListeners() called");
window.addEventListener('message', parseMessage, false);//listens for messages from the index.html file page
}
function parseMessage(event){
console.log("parseMessage() called");
}
addListeners() called 已记录,但 parseMessage() called 未记录。
我已经试过了:
更改函数的顺序。
在打开子窗口时发布消息。
例如:
var newWindow = window.open("file.html").postMessage("message string", '*');
【问题讨论】:
-
它不能解决你的问题,但我认为这个
window.onload = addListeners();应该是这样的:window.onload = addListeners; -
顺便说一句,我认为您应该删除
window.onload部分,也许由于某种原因它没有被调用,您实际上不需要等待窗口加载。 -
仅供参考,家长可以直接与孩子交谈
childWindow.document.等......
标签: javascript