This codepen is used to be embedded and show how to send message to its parent page
Created
September 30, 2022 09:15
-
-
Save zipang/4f835c77ec35f9b3a72b052cc4bd636e to your computer and use it in GitHub Desktop.
IFRAME MESSAGES SENDER
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
<div class="container"> | |
<h1>1 AM NOT YOUR STANDARD IFRAM3</h1> | |
<button id="cmdSendRandomQuote" class="basic-button"> | |
Click here to receive some random quote | |
</button> | |
<button id="cmdGiveRandomBeer" class="basic-button"> | |
Click here to have some beer | |
</button> | |
<h2>Last message sent</h2> | |
<p><ode><pre id="lastMessage"></pre></code></p> | |
</div> |
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
const lastMessage = document.getElementById("lastMessage"); | |
const postMessageFrom = (apiUrl) => (evt) => { | |
fetch(apiUrl) | |
.then((resp) => resp.json()) | |
.then((randomData) => { | |
console.log("Sending", randomData); | |
window.parent.postMessage(JSON.stringify(randomData), "https://cdpn.io/zipang"); | |
lastMessage.innerHTML = JSON.stringify(randomData, null, ' '); | |
}); | |
}; | |
document | |
.getElementById("cmdSendRandomQuote") | |
.addEventListener( | |
"click", | |
postMessageFrom("https://dummyjson.com/quotes/random") | |
); | |
document | |
.getElementById("cmdGiveRandomBeer") | |
.addEventListener( | |
"click", | |
postMessageFrom("https://random-data-api.com/api/v2/beers") | |
); |
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
* { | |
font-family: serif; | |
} | |
.container { | |
max-width: 60ch; | |
margin: 0 auto; | |
min-height: 100vh; | |
} | |
.basic-button { | |
padding: 1em; | |
margin: 1em auto; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment