Created
October 3, 2012 05:43
-
-
Save snowman-repos/3825251 to your computer and use it in GitHub Desktop.
JavaScript: window.postMessage API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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