I find my self in a javascript function as a result of a onload clause in the body of a page. How do I determine the name of the frame I am in? I don't know the id.
A:
It's been a long time since I've worked with framesets, but I think what you need is loop through the frames in the parent document until you find that the frame is the same as the document's this global object. Once you know this, just get the id of the tag normally. One way of doing this is:
for (var i = 0; i < parent.frames.length; i++) {
if (parent.frames[i] === this) {
alert(parent.frames[i].id)
}
}
Andrew
2010-09-02 02:08:03
@andrew: typo === I added alerts to display this.name (blank), this.id (undefined) parent.frames[i].name (frame name displayed)
Mike D
2010-09-02 13:14:52
Ah === is JS operator but it doesn't work either way. It's as if the .frames does not include stuff like id and title. Behavior the same on mozilla, IE, and opera.
Mike D
2010-09-02 13:41:09
Strange. It worked for me. What browser are you testing with? Do you have a sample live anywhere online?
Andrew
2010-09-02 16:44:13
Actually, I think what you need is just `this.name` in the frame documents. That appears to work for me.
Andrew
2010-09-02 17:43:04
@Andrew: The main page is at http://users.litewire.net/mmidorl/frames/xxx.htm. It uses font.js, gstyle.css, navigation.htm, introduction.htm, and index.htm. Your code with a bunch of alerts are found near the bottom in function mydefaultsize which is called from body onload by both frames in xxx.htm. I've tried in with IE 8, Firefox 3.0.19 and Opera 10.61. I'd like to know what I'm doing wrong but I have another approach in mind via passing a parameter.Note that both frame pages call the parent javascript. There is no script defined in the frame pages.
Mike D
2010-09-02 19:44:39
@andrew: Pretty much everything clears up if I change the frame pages to include definition of the javascript and change the onload clause from parent.function() to function(). It looks to me that context gets all screwed up if I use the parent.function(). I guess it's not a big deal but that means a few thousand web pages get the extra script link.
Mike D
2010-09-03 23:54:33