Wolfgang Adamec
Wolfgang Adamec

Reputation: 8534

Is there a way to get the iframe dom element from window.frames[0]

I always thought (when you have several iframes), that window.frames[0] represents the DOM element of the first iframe.

But according to the Mozilla Developer Network, this isn't true:

Each item in the window.frames pseudo-array represents the window object corresponding to the given 's or 's content, not the (i)frame DOM element (i.e., window.frames[ 0 ] is the same thing as document.getElementsByTagName( "iframe" )[ 0 ].contentWindow).

Is there a way in JavaScript to get from window.frames[0] to the DOM element of the iframe?

Upvotes: 1

Views: 698

Answers (1)

Julien Grégoire
Julien Grégoire

Reputation: 17144

frameElement will return the DOM element in which the iframe is embedded. https://developer.mozilla.org/en-US/docs/Web/API/Window/frameElement

For example:

var iframeElem = window.frames[0].frameElement

Following, this should validate:

window.frames[0].frameElement.ownerDocument.defaultView.frames[0] == frames[0]

Upvotes: 3

Related Questions