Skip to content

Instantly share code, notes, and snippets.

@veewee
Created March 24, 2015 09:54
Show Gist options
  • Save veewee/3fa8f6e5f680b41bb36b to your computer and use it in GitHub Desktop.
Save veewee/3fa8f6e5f680b41bb36b to your computer and use it in GitHub Desktop.
PostMessage tester
<html>
<head>
<script>
var count=0;
function sendMessage() {
window.parent.postMessage(
JSON.stringify({"greeting": "hello world"}), // A stringified message to send.
"*" // The intended origin, in this example we use the any origin wildcard.
);
}
window.onmessage = function(e){
if(e.origin === window.location.origin){
return;
}
if(!e.data){
throw new Error('Message event contains no readable data.');
}
var elm = document.createElement('li');
elm.innerHTML = e.data;
var out = document.getElementById('messages');
out.appendChild(elm);
}
</script>
</head>
<body>
<p>
child window on different domain <a href="https://gist.github.com/kylewelsby/585b3a5395c6731acc50">GitHub Gist</a>
<button type="button" onclick="sendMessage()" style="float: right;">Send message!</button>
</p>
<ol id="messages"></ol>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment