How to communicate between an iframe and the parent page

From parent page -> iframe

In the parent page:

const iframe = document.getElementById('iframe'); 
iframe.contentWindow.postMessage('some message', '*');

In the iframe:

window.onmessage = function(e) {
if (e.data === 'some message') {
alert('It works!');
}
};

From iframe -> parent page

In the parent page:

window.onmessage = function(e) {
if (e.data === 'from iframe') {
alert('It works!');
}
};

In the iframe:

window.top.postMessage('from iframe', '*')

Source

--

--

--

https://alexadam.dev/

Love podcasts or audiobooks? Learn on the go with our new app.

Razorpay Integration In Flutter Web

The dev.to project with GatsbyJS-19

CSS : Important Points

Here is a very long GRID document

Chaining CSS Animations using Javascript.

Animating ball

The Publish-Subscribe Pattern in JavaScript

Why I Don’t “Love” NextJS

👋 Hello world, we are Future Pi.🌎

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
alex adam

alex adam

https://alexadam.dev/

More from Medium

JavaScript Coding Tutorial — Part 6

By changing just two functions we can get some pretty nice paint brush effects already.

Remove extra whitespace caused by line break within a button

EmojiScript: A Quick Guide to Emojis in JavaScript

Creativity JS style