dude dude - 1 month ago 4x
HTML Question

Get parent iframe element by an element located within an iframe

Assuming you have the following HTML:

<iframe src="..">
<!-- The following would be the content of the iframe -->
<span>I'm the node</span>

And you have a variable
containing the
element within that iframe. Your script context is the parent window.

How would you get the parent iframe element:

<iframe src="..."></iframe>

by this node?

In theory:

var iframe = iframeNode.myParentIframeElementInTheParentScope;


If your only reference to the iframe is a span in that iframe (= iframeNode), then

var iframe = iframeNode.ownerDocument.defaultView.frameElement;

will refer to the iframe element in the parent window.