Skip to content

Instantly share code, notes, and snippets.

@raulmangolin
Created January 6, 2017 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raulmangolin/5aed184eb91d3d6da90686e9c9e1dd64 to your computer and use it in GitHub Desktop.
Save raulmangolin/5aed184eb91d3d6da90686e9c9e1dd64 to your computer and use it in GitHub Desktop.
postMessage example
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>postMessage Example</title>
<script type="text/javascript">
function openWindow(){
window.open("popup.html", "mywindow", "width=350,height=250");
}
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
eventer(messageEvent,function(e) {
console.log('origin: ', e.origin)
console.log('parent received message!: ', e.data);
}, false);
</script>
</head>
<body>
<button onClick="javascript:openWindow();">Open window</button>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Popup</title>
<script>
function sendMessage() {
window.opener.postMessage({message: 'work!!!'}, 'http://localhost/');
}
</script>
</head>
<body>
<button onClick="javascript:sendMessage();">Send message</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment