Skip to content

Instantly share code, notes, and snippets.

@sangar82
Last active December 16, 2015 03:29
Show Gist options
  • Save sangar82/5370300 to your computer and use it in GitHub Desktop.
Save sangar82/5370300 to your computer and use it in GitHub Desktop.
window.postMessage is a method for safely enabling cross-origin communication. Normally, scripts on different pages are only allowed to access each other if and only if the pages which executed them are at locations with the same protocol (usually both http), port number (80 being the default for http), and host (modulo document.domain being set…
Domain A with iframe:
-----------------------
<iframe></iframe>
<script>
window.addEventListener( "message",
function (e) {
if(e.origin !== 'http://www.domainB.com'){ return; }
alert(e.data);
},
false);
</script>
Domain B, content for the iframe
-----------------------
$(window).load() {
top.postMessage("message", '*');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment