Created
January 6, 2017 17:45
-
-
Save raulmangolin/5aed184eb91d3d6da90686e9c9e1dd64 to your computer and use it in GitHub Desktop.
postMessage example
This file contains hidden or 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
<!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> |
This file contains hidden or 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
<!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