Sara Chipps
Sara Chipps

Reputation: 9372

Finding an iframe within another iframe?

I have the id of the parent, but not the child iframe.

Is there a way to return the iframe within the parent iframe?

Or, all iframes within a particular iframe?

Using jQuery to find element with $('iframe') doesn't seem to work.

Upvotes: 3

Views: 396

Answers (2)

felickz
felickz

Reputation: 4461

var childFrames = frames['parentId'].frames

Make that recursive and start from top.frames might suit your needs as well... and has a lot less of the DOM heavy "getElementsByTagName"

or if you are in the child i frame but you just need to get a handle on the frame, this gets you the id: window.frameElement.id

**only tested in IE :(

Upvotes: 0

alex
alex

Reputation: 490657

If the iframe's src attribute has the same domain, protocol and port, you are set. If not, you can't do anything because of Same Origin Policy.

Assuming you are not violating the policy...

Regular JavaScript

var iframe = document.getElementsByTagName('iframe')[0],
    iframeDocument = iframe.contentWindow || iframe.contentDocument,
    internalIframes = iframeDocument.getElementsByTagName('iframe');

You need to use the || short circuit evaluation exploit as IE is different.

jQuery

var internalIframes = $('iframe:first').contents().find('iframe');

Upvotes: 4

Related Questions