Solved

Finding a frame by name

Posted on 1997-05-19
4
296 Views
Last Modified: 2012-05-04
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?
0
Comment
Question by:mkornell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:RM032397
ID: 1265602
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
 
LVL 3

Accepted Solution

by:
RM032397 earned 100 total points
ID: 1265603
See above!
0
 
LVL 2

Author Comment

by:mkornell
ID: 1265604
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
 
LVL 2

Author Comment

by:mkornell
ID: 1265605
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question