-
-
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)
This file contains 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>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> |
This file contains 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> | |
</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