Skip to content

Instantly share code, notes, and snippets.

@snowman-repos
Created October 3, 2012 05:43
Show Gist options
  • Save snowman-repos/3825251 to your computer and use it in GitHub Desktop.
Save snowman-repos/3825251 to your computer and use it in GitHub Desktop.
JavaScript: window.postMessage API
// From window or frame on domain 1, send a message to the iframe which hosts another domain
var iframeWindow = document.getElementById("iframe").contentWindow;
iframeWindow.postMessage("Hello from the first window!");
// From inside the iframe on different host, receive message
window.addEventListener("message", function(event) {
// Make sure we trust the sending domain
if(event.origin == "http://davidwalsh.name") {
// Log out the message
console.log(event.data);
// Send a message back
event.source.postMessage("Hello back!");
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment