0

I have the following html code:

<div id="container">
   <h1> here is the iframe </h1>
   <iframe src="...."></iframe>
</div>

I need to be able to get the id of the div that the iframe is in (id="container") from within the iFrame. Is this possible?

Thanks!

  • Check out [this question](https://stackoverflow.com/questions/2620755/how-is-it-possible-for-an-iframe-to-access-its-parents-dom). – ChrisG May 24 '21 at 14:28
  • Thanks, I didn't see anything there that can be useful in my case. – Avi Boteach May 24 '21 at 14:49
  • 1
    The short answer is no. The long answer, as explained in the answer on that question, is that if they are same origin, you should be able to access the `parent` property and go on from there. – ChrisG May 24 '21 at 19:06

1 Answers1

0

what I did at the end is to make the frame id, a function of its container id. for instance:

<div id="container1">
    <iframe id="container1_iframe" src="..."></iframe>
</div>

then, from within the iFrame I got the iframe id like this: window.frameElement.id

and from its value I deducted the container id.

  • 1
    Should not need the detour via the ID, if you can access the iframe element via `window.frameElement`, then you should also be able to access its parent directly via `window.frameElement.parentNode` – CBroe May 25 '21 at 09:21