-
-
Save FlorianRappl/dddbf80773ed8df03fcf20679f30c835 to your computer and use it in GitHub Desktop.
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>Microfrontends Shell</title> | |
</head> | |
<body> | |
<h1>Parent</h1> | |
<p><button id="message_button">Send message to child</button></p> | |
<div id="results"></div> | |
<script> | |
const iframeSource = "https://example.com/iframe.html"; | |
const iframe = document.createElement("iframe"); | |
const messageButton = document.querySelector("#message_button"); | |
const results = document.querySelector("#results"); | |
iframe.setAttribute("src", iframeSource); | |
iframe.style.width = "450px"; | |
iframe.style.height = "200px"; | |
document.body.appendChild(iframe); | |
function sendMessage(msg) { | |
iframe.contentWindow.postMessage(msg, "*"); | |
} | |
messageButton.addEventListener("click", function(e) { | |
sendMessage(Math.random().toString()); | |
}); | |
window.addEventListener("message", function(e) { | |
results.innerHTML = e.data; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment