Created
March 24, 2015 09:54
-
-
Save veewee/3fa8f6e5f680b41bb36b to your computer and use it in GitHub Desktop.
PostMessage tester
This file contains 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
<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