Marius
Marius

Reputation: 58911

How do I get a reference to the iframe the document is loaded inside?

I need to get the DOM iframe element on a page from inside the iframe. Both documents are from the same domain, so don't worry about cross-origin restrictions. The document inside the iframe needs to put an even-listener on the iframe which it is loaded inside. This should work even if there are multiple frames in the parent document.

edit:

window.parent will get the parent document. Now I need to get one of the iframes in the parent document. window.parent.frames is a list of those iframes. The question now is how to get the correct iframe from that list.

Upvotes: 0

Views: 95

Answers (1)

Guffa
Guffa

Reputation: 700192

You can use the window.frameElement property to get the element that contains the page. This is only standardised in HTML5 though, so it's not supported in all browsers.

For a cross browser solution you would use the window.parent property to get the parent window, then locate the iframe element in it. You would need a way to identify the iframe, for example the id of the element:

var frame = window.parent.document.getElementById('#frame');

Upvotes: 2

Related Questions