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].frames[6].Obviously, window.open() can find a frame by name; how can some javascript do the same?
LVL 2
mkornellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RM032397Commented:
No doubt you have a serious application in mind. Let me give you a warning - and I would be interested to hear from others with similar experiences.  

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
0
RM032397Commented:
See above!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mkornellAuthor Commented:
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.
0
mkornellAuthor Commented:
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")}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.