window.postMessage Tip: Child-To-Parent Communication
I wrote a super epic post a few months back about the window.postMessage
API that's sweeping the nation. window.postMessage
allows you to send messages not only across frames (regular frame or iframe) but also across domains. My post showed interaction from parent to child and back to the parent, but didn't detail passing messages from a child to a parent without the parent initializing the conversation. Let me show you how you can initialize that conversation from child to parent
The JavaScript
The parent object provides a reference to the main window from the child. So if I have an iFrame and console the parent within it, the console will read:
// Every two seconds.... setInterval(function() { // Send the message "Hello" to the parent window // ...if the domain is still "davidwalsh.name" parent.postMessage("Hello","https://davidwalsh.name"); },1000);
Since we now have hold of the window, we can postMessage to it:
// Create IE + others compatible event handler var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; var eventer = window[eventMethod]; var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; // Listen to message from child window eventer(messageEvent,function(e) { console.log('parent received message!: ',e.data); },false);
The directive above triggers the iFrame to send a message to the parent window every 3 seconds. No initial message from the main window needed!
Great article, It worked great on iFrame resizing…WOO HOO! after many failed posts and other methods, this one was so simple.
Hey,
So I’ve scoured the usual forums for a solution to cross-domain iframe communication, and nothing has worked. Not copy/paste, not my own code, not even on the same domain. A lot can change in a year ( haven’t found an article published in 2012 that tells me how to implement this) but I am in deep need of a sanity check.
–SWJ
Hi,
My outside of iframe facny box is not working in chrome.so how to solve that problem.
Hi David,
Thanks man. I’ve been looking for a solution for iframe resizing across domains since yesterday. I’ve finally found it.
Really appreciate it.
Cheers
Hi,
I am getting an error:
Uncaught TypeError: Object # has no method 'postMessage' even though I am able to get the iframe and its details.
Hey, it looks and works great but on IE, action passed received is undefined.
but here this Action: RESIZE is undefined in postmessage on IE
This is because some browsers only support strings, so you’d want to use
JSON.stringify()
andJSON.parse()
to deal with sending any kind of data structure that isn’t a string.How do i post the message from iframe which contains https page to a parent page which is http? Even the domains are different? Any idea?
It just works :)
Some browsers supports only ‘string’ message than any other objects(Eg:JSON)
You can simply use
Could you do it like this also?
The way you specified doesn’t work as intended because jQuery puts a wrapper around the returned event object (in your case
e
) to wheree.data
is not defined as it is in the article’s examples. For this case, it’d be better to just step outside of jQuery for a moment and use the pure Javascript implementation.Hi
Can postmessage work for HTTPSHTTPS communications?
Thanks, it was very useful and simple.
How to pass message to child ifrmae? I want to get cookie form 3rd party website to child iframe. I have access over script at 3rd party throught which i register iframe and some functions at client end. how to access cookie?
Thanks.
Hi did you find a solution for your question. I also have to do the same
How to do parent-to-child communication ?
Thanks for the post. I am facing issue on parent window. The code
is not called at all.
Nice code, but in IE i get an error in the console:
SCRIPT5: permission denied.
column 1, char 1
why is there no
like in your other code here: https://davidwalsh.name/window-postmessage ?
Works! Thanks!
Why did you say “send a message to the parent window every 3 seconds” ? I don’t see any interval or timeout in your code sending anything every 3000ms…
This was so helpful. I had to pass a message from a react app contained within an iframe to a parent. Works great!