const targetFrame = window.top.frames[1];
const targetOrigin = "https://example.org";
const windowMessageButton = document.querySelector("#window-message");
windowMessageButton.addEventListener("click", () => {
targetFrame.postMessage("hello there", targetOrigin);
});Window: message event / notebook
Window: message event
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
window.addEventListener("message", (event) => {
console.log(`Received message: ${event.data}`);
});window.onmessage = (event) => {
console.log(`Received message: ${event.data}`);
};