Created
April 24, 2017 16:20
-
-
Save wilfrem/6c57ea855eca4b76b7e469ba40a77672 to your computer and use it in GitHub Desktop.
Module using postMessage
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
<html> | |
<body> | |
<script> | |
if (window.opener) { | |
window.addEventListener("message", function(ev) { | |
var dataStr = ev.data; // get data from parent | |
// calc and get data | |
var result = somethingYouWantToDo(parseDataString(dataStr)).stringify(); | |
window.opener.postMessage(result, "*"); | |
}); | |
} | |
</script> | |
</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
var moduleIframe = document.createElement("iframe"); | |
moduleIframe.src = "uri/to/module"; | |
// add iframe | |
document.body.appendChild(moduleIframe); | |
// after onload | |
// post message | |
moduleIframe.contentWindow.postMessage(dataStr, "module.html served domain"); | |
// receive message | |
window.addEventListener("message", function(ev) { | |
// must check origin | |
if (ev.origin !== "module.html served domain") { | |
return; | |
} | |
// get result | |
ev.data; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment