I have several iframes with same origin (but different pathnames) on the page.
Every iframe emits message
event through postMessage
.
Parent window listens these events:
window.addEventListener('message', function(event) {
/* Get iframe element by event */
});
I want to get source iframe element for every event.
The important limitation is that I have no access to event.source.contentWindow
because of cross-origin.
UPD: answer below
If sending a message to a document in an iframe, first get a reference to the iframe and then its contentWindow property as shown below. If sending a message from the iframed document to the containing document, the parent keyword provides a reference. If sending a message to a window opened using the window.
Communicating directly between iframes is also possible by combining window. parent with target as defined above. In conclusion, the postMessage method is a more dynamic alternative to the single DOM, better suited if you load multiple pages in one iframe, but not always easier and it still requires the use of the DOM.
postMessage() is a safe way to send messages between windows in different domains or origins. One can also post to an IFrame. The data being sent is serialized using the structured clone algorithm and will accept almost any type of simple or complex data.
The solution is to compare event.source
and iframe.contentWindow
:
function getFrameByEvent(event) {
return [].slice.call(document.getElementsByTagName('iframe')).filter(function(iframe) {
return iframe.contentWindow === event.source;
})[0];
}
Here's more modern version:
function getFrameByEvent(event) {
return Array.from(document.getElementsByTagName('iframe')).filter(iframe => {
return iframe.contentWindow === event.source;
})[0];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With