Skip to content

Instantly share code, notes, and snippets.

@nguyentienlong
Forked from mbajur/index.html
Created October 5, 2016 16:41
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 nguyentienlong/f73ce25877834ce83bfe41c810bca899 to your computer and use it in GitHub Desktop.
Save nguyentienlong/f73ce25877834ce83bfe41c810bca899 to your computer and use it in GitHub Desktop.
Working example of window.postMessage used for sending data from popup to parent page (works in IE)
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
<script type="text/javascript">
window.open ("popup.html","mywindow", "width=350,height=250");
// Create IE + others compatible event handler
var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";
// Listen to message from child window
eventer(messageEvent,function(e) {
console.log('origin: ', e.origin)
// Check if origin is proper
if( e.origin != 'http://localhost' ){ return }
console.log('parent received message!: ', e.data);
}, false);
</script>
</head>
<body>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Popup</title>
</head>
<body>
<script type="text/javascript">
window.opener.postMessage({token: 'aaa', secret: 'bbb'}, 'http://localhost');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment