xxxxxxxxxx
<html>
<script type="text/javascript">
function click2() {
console.log("click");
window.framePort.postMessage("Click Message From Parent");
}
function load() {
console.log("load index");
window.addEventListener("message", onMessage, false);
window.postMessage("test message", "*");
}
function onFrameMessage(e) {
console.log("In Parent");
console.log(e);
}
function onMessage(e) {
console.log(e);
if (e.data === "channel") {
console.log(e.ports[0]);
window.framePort = e.ports[0]
window.framePort.onmessage = onFrameMessage
}
}
</script>
<body onload="load()">
<button onclick="click2()">Send To Frame</button>
<iframe src="frame.html"></iframe>
</body>
</html>