Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

which iframe did I find in the frames array?

7:32 pm on Aug 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Aug 1, 2003
votes: 0

I'd like to know how I can determine if I can take the information I have when I find a frame in the frames array and reverse lookup to find out which iframe that is.

When I debug through the frames loop and look at the properties returned by document.frames[x] it's nothing at all like the properties returned by a loop through getElementsByTagName('iframe').item(x).

So suppose I have an undetermined number of iframes on a page, and an undetermined number of frames. I already have a way of knowing that a particular frame in the loop is my iframe (explained later) but I can't work out which iframe that is. I need to know which iframe it is so I can get its offsetLeft and offsetTop properties. A frame reference returned by the frames array doesn't offer that information.


I need to find the offsetLeft and offsetTop positions of an iframe on a page so I can write a layer to that page in a position relative to the iframe. The iframe has no ID tag (and I have no way of ensuring it has one) or if it does, I don't know what it is. This iframe that I'm looking for is my pages great great grandparent (4 layers of nested iframes where the top and bottom iframes are in the same domain). So the great grandchild iframe has all rights to read/write in the great grandparent iframe. There's no problems there. The problem is finding out where my great grandparent is on the page so I can write next to it.

What I can already determine though is which element in the frames array is my iframe. I do this by getting a reference to it in the great grandchild iframe by doing "var grandparent=parent.parent". That gives me a 'reference' to the iframe that I'm trying to find on the great grandparent page. Now if I loop looking at parent.parent.parent.frames[x] and compare that reference with the 'grandparent' variable reference I find a match in the loop. The references are both to the same object so they match.

So now I know which element in the frames array of the great grandparent is my grandparent. Now if I could just ask it then what its offsetLeft or offsetTop are I'd be all set, but it's a frame at this point, not an iframe, because it's in the frames array, and a frame doesn't have those properties. What I'd need to do somehow is cast the reference to the frame to be a reference to an iframe and I'd get all of its properties. Alternately, find a way of determining which of the iframes corresponds to that frames array entry. Then I could access that iframe directly using getElementById (or something) and then I find the offsets.

When I look at the frames reference object it has very few properties. It has undefined location.href and if I try to overwrite it's 'name' and then look in the iframes array to find that name I have no luck.

So how can I take this found frame and reverse engineer the knowledge to find out which iframe "item" number it is from the getElementsByTag('iframe') loop? Because once I find it via an iframe loop also, I can ask it for its offset values.


8:02 pm on Aug 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 15, 2003
votes: 5

That's a tough one, how about creating a new Iframe object:

var newIframe=document.createElement("iframe"); 

-and then assigning the id of the frames[x] to the iframe? perhaps something like this pseudocode:

var id = frames[10];
newIframe = document[id]

I'm sceptical myself, but it's my best (wild) guess right now.

BTW: Welcome to WebmasterWorld :)

8:57 pm on Aug 1, 2003 (gmt 0)

New User

10+ Year Member

joined:Aug 1, 2003
votes: 0

Thanks for that suggestion Claus, unfortunately it didn't work. It comes out as undefined.

I think the problem here is that I'm trying to find out something about a frame (i.e. anything that can give me a clue to which iframe it is) and the cross-domain security won't let me get access to it. I can't find out its location, position, name, domain or anything.

I just need to be able to cast that reference to the frame to be a reference to the iframe (which it really is). A cross-domain iframe object will tell you more about itself while still maintaining cross-domain integrity.

There may well not be a way around this, and I don't want to exploit any current loopholes that haven't been fixed yet. They have a habit of getting plugged. (Not that I know one here anyway).


3:45 pm on Aug 2, 2003 (gmt 0)

New User

10+ Year Member

joined:Aug 1, 2003
votes: 0

Found my solution. I was overcomplicating the problem (in my mind). You can never get frames and iframes mixed in the frames array. The frames array will either be full of real frames (on a frameset page which has no body and therefore no iframes) or it's full of iframes (from the body of a document which therefore can't have a frameset). Easy really.

So when I find a matching reference in the frames array in a loop, I know I've found the right index number of my iframe. I take my loop index and use it to get the getElementsByTagName.item(x) correct iframe. I can then ask it for its offsetLeft and offsetTop. This works fabulously :)


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members