Skip to content

Instantly share code, notes, and snippets.

@cirocosta
Last active January 6, 2024 23:02
Show Gist options
  • Save cirocosta/9f730967347faf9efb0b to your computer and use it in GitHub Desktop.
Save cirocosta/9f730967347faf9efb0b to your computer and use it in GitHub Desktop.
Sending messages from child iframe to parent webpage
<!DOCTYPE html>
<html>
<head>
<title>My Iframe</title>
</head>
<body>
<button>Botão</button>
<script type="text/javascript">
document.querySelector('button').onclick = function () {
// parent.postMessage("message to be sent", "http://the-website-that-will-receive-the-msg.com")
parent.postMessage("myevent", "*")
};
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<iframe src="iframe.html" width="300" height="300"></iframe>
<script type="text/javascript">
var eventMethod = window.addEventListener
? "addEventListener"
: "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod === "attachEvent"
? "onmessage"
: "message";
eventer(messageEvent, function (e) {
// if (e.origin !== 'http://the-trusted-iframe-origin.com') return;
if (e.data === "myevent" || e.message === "myevent")
alert('Message from iframe just came!');
console.log(e);
});
</script>
</body>
</html>
@gy0857478
Copy link

It is working w/o iframe ? use window.open instead of iframe

@Carlos-Trinidad
Copy link

It is working whit cross origin iframes?

@xerosanyam
Copy link

for future readers: this solution does not work with cross-origin.

@mgrn0
Copy link

mgrn0 commented Feb 3, 2020

it works cross-origin perfectly for me on latest Chrome.

@Carlos-Trinidad
Copy link

Yes it works. I have an app on production with this and it works cross-origin perfectly

@xerosanyam
Copy link

Yes it works. I have an app on production with this and it works cross-origin perfectly

I tested on firefox, was not working for me. Can you please verify?

@mgrn0
Copy link

mgrn0 commented Feb 5, 2020

Yes it works. I have an app on production with this and it works cross-origin perfectly

I tested on firefox, was not working for me. Can you please verify?

Yes, works flawlessly on current Firefox, too.

@Eldad7
Copy link

Eldad7 commented Jul 10, 2020

I can confirm it is working still. Thanks!

@hendri1
Copy link

hendri1 commented Aug 11, 2020

it can use for allow user media access?

@Eldad7
Copy link

Eldad7 commented Aug 11, 2020

it can use for allow user media access?

I believe you can try and use the main page to ask the user media access, and retrieve the IDs, then you can send the IDs to the iframe via a message and use it there.
Of course you need to make sure the Iframe is also under SSL because otherwise it wouldn't work..

@brunoinds
Copy link

Works perfectly! I didn't know parent can be used as global variable, like this are.

@insign
Copy link

insign commented Feb 5, 2021

Thank you so much! Saved the day!

@Skaaala
Copy link

Skaaala commented Feb 11, 2021

Perfect, it really works :)

@alexalannunes
Copy link

Niceeeeeeeeeeeeee

@muhammedmoussa1
Copy link

Thank you!

@luccafabro
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment