Skip to content

Instantly share code, notes, and snippets.

@gareth
Created August 4, 2016 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gareth/41cb1644638c6c956d61297f419b091b to your computer and use it in GitHub Desktop.
Save gareth/41cb1644638c6c956d61297f419b091b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Launcher</title>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(function() {
var receiver;
$('#launch').click(function() {
if(receiver) { receiver.close(); }
receiver = window.open('/target.html', 'target', 'width=200,height=200');
})
$('#send').click(function() {
if(receiver) {
receiver.postMessage("Hello!", "*")
}
})
})
</script>
</head>
<body>
<button id='launch'>Launch!</button>
<button id='send'>Send!</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Target</title>
<style>
p { background: #eee; padding: 5px; font-family: sans-serif; }
</style>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var count = 0;
window.addEventListener("message", function(e) {
$('body').prepend(
$('<p>').text(++count + ' ' + e.data)
);
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment