This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes iFrames - accessing elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "iFrames - accessing elements" Watch "iFrames - accessing elements" New topic

iFrames - accessing elements

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830

It's related to iFrames.

Say I have two iFrames in a page which renders (embeds) two other pages. Now,

I can access element of an iFrame in parent page like this:


where aFrame is name of one of the frames.

I can access element of an iFrame in another iFrame like this:


So far it's working in all the major browsers (IE, FireFox & Chrome).

Now what if I include pages of two different domain e.g.

Instead of this:

<iframe src="f1.html" name="aFrame" />
<iframe src="f2.html" name="bFrame" />

I'll have:

<iframe src="" name="aFrame" />
<iframe src="" name="bFrame" />

Will I be able to access elements of an iFrame in parent page or in other iFrame?

I heard that browsers prevent this due to some security reasons. I also don't get alert(document.aFrame.document) when aFrame iFrame contains say or but I get an [object] in alert when aFrame contains f2.html (a page in same domain).

What if I include a sub domain page in an iFrame which is in a page of parent domain e.g. has a page contains iFrame and this iFrame refers a page of (sub domain of Will I be able to access each others elements?

My main problem is I don't know how do i simulate this sub domain situation. I don't own any domain. Is it possible to do it locally?


Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
How to simulate a sub domain?

DO you have firebug? Use the console or the address bar to inject an iframe into the page with createElement and appendChild. This way you can add an iframe into the page with a site that has subdomains.

I agree. Here's the link:
subject: iFrames - accessing elements
jQuery in Action, 3rd edition