Possible to get data from an iFrame?

I’m using Ionic 4, and launching a Google Map in an iframe. The map has a bunch of markers on it, and when the user clicks a marker it fires a javascript event in that iframe.

Is there some clever way to get details about what they clicked? I’m trying to get their chosen location.

Thanks for any help.

1 Like

No, there’s no way to access data inside a cross-origin iframe except if the iframe uses postMessage(). If you control that iframe, that’s a completely different story, you can do whatever you want then. If it’s the same origin, you can traverse between window objects. If it’s not the same origin, you can use postMessage inside the iframe to emit data

2 Likes

Yup, I control the iframe. That iframe is on a remote webserver (my webserver), does that put it on a different origin?

Investigating PostMessage() now, thanks for that!

Yes that would be a cross-origin situation, but what you can do then is handle those events in your iframe, then emit them using postMessage. Then, in your parent window (i.e. your app) you can use

window.addEventListener("message", (event) => {

To handle the message

2 Likes