Skip to content

Instantly share code, notes, and snippets.

@wilfrem
Created April 24, 2017 16:20
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 wilfrem/6c57ea855eca4b76b7e469ba40a77672 to your computer and use it in GitHub Desktop.
Save wilfrem/6c57ea855eca4b76b7e469ba40a77672 to your computer and use it in GitHub Desktop.
Module using postMessage
<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>
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