Skip to content

Instantly share code, notes, and snippets.

@fabioyamate
Last active December 18, 2015 19:19
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 fabioyamate/5831796 to your computer and use it in GitHub Desktop.
Save fabioyamate/5831796 to your computer and use it in GitHub Desktop.
postMessage
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>postMessage</title>
</head>
<body>
<div id="container">
iFrame
<button>post message</button>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$('button').on('click', function() {
window.parent.postMessage('post data message', 'https://rawgithub.com');
});
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>postMessage</title>
</head>
<body>
<div id="container">
<iframe src="https://rawgithub.com/fabioyamate/5831796/raw/iframe.html" id="iframe" border="1"></iframe>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(window).bind('message', function(e) {
var event = e.originalEvent;
alert('origin: ' + event.origin);
alert('data: ' + event.data);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment