简体   繁体   中英

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 iframe s within a particular iframe ?

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

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');

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:(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM