mkornell
asked on
Finding a frame by name
How does one find a frame object in the frame/window hierarchy, given its name. The frame may be top.frames[0], or top.frames[8].frames[4].fr ames[6].Ob viously, window.open() can find a frame by name; how can some javascript do the same?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I want to get the frame object for the desired frame, so I can do things to it.A trivial example:f = find_frame_by_name ("frame_named_fred");f.document.writeln ("<P><FONT color=red>Fred is red</FONT></P>");Your comments re: frame fatigue are noted. I'm not actually creating any frame hierarchy that big. Its more like frames[4].frames[2].I was hoping there was a built-in way to find the frame. Guess I'll have to write the recursive function you described.
ASKER
For those interested, here is a bit of JS code that finds a frame by name.function findWindow (winRoot, strName) {
var aWin
var len
var i
if (winRoot.name == strName) {
return winRoot
} else {
len = winRoot.frames.length
for (i = 0; i < len; i++) {
aWin = findWindow (winRoot.frames[i], strName)
if (aWin != null) {
return aWin
}
}
}
return null
}
use it as such:var win = findWindow (top, "frame_named_fred")if (win != null) { win.document.writeln ("I found frame_named_fred")}
var aWin
var len
var i
if (winRoot.name == strName) {
return winRoot
} else {
len = winRoot.frames.length
for (i = 0; i < len; i++) {
aWin = findWindow (winRoot.frames[i], strName)
if (aWin != null) {
return aWin
}
}
}
return null
}
use it as such:var win = findWindow (top, "frame_named_fred")if (win != null) { win.document.writeln ("I found frame_named_fred")}
I find (in Netscape 3.0 which is my main development and test platform) that when testing applications with several frames, and I am NOT talking DOZENS as implied by your question that after several reloads during the usual cycle of develop and test the following occurs:
I call it 'Frame Fatigue'. The frame structure just stops working. Frames that should load do not. That is it. The only cure is to kill the browser and fire it up anew.
Now that you know this I hope you may reconsider the extravaganza of frames.
Now as for your question, please specify what 'finding the frame' constitutes and what the circumstances are in which this finding exercise must take place. Is it for a game? If not then the reaction is better not to lose than to have to find.
You would have to write a loop that iterated through the frames array to its length. For each frame/window in the array you would test the length of its own frames array and recursively loop into it if the lenght is > zero. The function MUST be recursive because you could not decide in advance how many levels to go down.
This started out a comment, but I think it just became an answer1