.postMessage() Example - Sender
by Gerald Gillespie
HTML
<textarea id="text"></textarea>
<button id="btn">Send Message</button>
<!-- uncomment for iframe example -->
<iframe width="100%" height="400" src="https://jsfiddle.net/gillyspy/8ybn6stk/2/show" frameborder="0" id="child"></iframe>
JavaScript
//const childWindow = window.open("https://jsfiddle.net/ozzan/wmxf9fs2/show", "_blank");
//uncomment for iframe example
var btn = document.getElementById("btn"),
text = document.getElementById("text");
btn.addEventListener("click", function() {
sendMessage(text.value);
text.value = "";
});
function sendMessage(message) {
const childWindow = document.getElementById("child");// .contentWindow;
// console.log('cw',childWindow);
if (!message || !message.length) return;
childWindow.contentWindow.postMessage(JSON.stringify({
message: message,
time: new Date()
}), "https://jsfiddle.net/");
console.log("sent");
}