I have a prototype representing a particual IFrame. That prototype have a function called GoToUrl(...) that opens the given url within the IFrame.
My question is: How do I create an "InternalDOM" property and make this property refer to the "window" object (the root DOM object) of the IFrame inside? In such way that: If my IFrame exposes a page which has an object X in it's "window" object I could do:
MyFrameObject.GoToUrl(pageXurl);
MyFrameObject.InternalDOM.X
Any help would be appreciated.
PS: I would accept answers not necessarily related to jQuery but I would prefer a jQuery solution.
To get the
window
object for a frame you can use thewindow.frames
array:This requires that you give the
<iframe>
an old-schoolname
attribute instead-of-or-as-well-as theid
. Alternatively if you know the order of iframes on the page you can index them numerically:It's generally more flexible to get the iframe window from the iframe element in the DOM, but this requires some compatibility code to cope with IE:
jQuery defines the contents() method to grab the
document
node, but it doesn't give you a cross-browser way to go from thedocument
to thewindow
, so you're still stuck with:which isn't really a big win.
(Note: be very careful using jQuery for cross-frame-scripting. Each frame needs its own copy of jQuery and methods from one frame's copy won't necessarily work on nodes from the other. Cross-frame-scripting is a topic fraught with traps.)
To sum it up
Access iframe content from parent page
Access parent page content from iframe
This applies only when the parent and the iframe pages are on same domain.
EDIT: On load child iframes example:
parent html
parent js
All major browsers including IE9 work with the
on('load')
lines. Only IE8/7 need the interval block.UPDATE @RoyiNamir 's comment:
for window in a iframe.
for document in a iframe
for body in a iframe
for body in a iframe with jquery
IMPORTANT: you should always check that the document is with the status "complete" for work with this