Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding a frame by name

Posted on 1997-05-19
4
Medium Priority
?
298 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
  • 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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

972 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