[Javascript] Tricky frame problem.

Roger Roelofs rer at datacompusa.com
Thu Jan 26 03:22:24 CST 2006


Rick,

On Jan 25, 2006, at 11:31 PM, Rick Holcomb wrote:

> I have a frameset that contains two frames a top and a main frame.
> I use the top as a menu and load all the pages into the main frame.
> My problem is that I have designed a new page that contains frames 
> itself.
> The frames in the new page are topFrame, leftFrame and mainFrame.
> When I post values from mainFrame to leftFrame using a script in 
> topFrame,
> everything works fine. But once I add this frameset to the first 
> frameset
> all hell brakes loose. So main will now conatin topFrame, leftFrame and
> mainFrame.
>
> The javascript call I make from mainFrame to topFrame is:
>
> parent.passText2('value')
>
> The script in topFrame that posts the value to leftFrame is:
>
> function passText2(str) {
> if (top.frames['leftFrame'].document.theForm.txtBucket.value == "")
>    {
>    top.frames['leftFrame'].document.theForm.txtBucket.value =
> top.frames['leftFrame'].document.theForm.txtBucket.value + str;
>    }
> else
>    {
>    top.frames['leftFrame'].document.theForm.txtBucket.value =
> top.frames['leftFrame'].document.theForm.txtBucket.value + "," + str;
>    }
> }

Is there any chance of putting up a demo page for us?  The frame names 
are kind of generic so I'm having a little trouble following the flow 
of your description.  Here are some general comments that might point 
you in the right direction.

The javascript needs to know about the frame hierarchy.  Also, keep in 
mind that the javascript function runs in the context in which it was 
declared, not from where it is called.  I haven't done this kind of 
cross-frame call in years, but I recall that it was often most useful 
to link the script into the frameset document.  Calling a script linked 
into the frameset from a child frame becomes parent.theFunction().  If 
the javascript lives in another of the child frames, then you call it 
as parnet.frames['topframe'].theFunction()

The difference between 'parent' and 'top' is that 'top' points to the 
topmost frameset while 'parent' only goes up one level.

You can save yourself a lot of typing by putting an object reference 
into a variable like so...

function passText2(str) {
   var dest = parent.frames['leftFrame'].document.theForm.txtBucket;
   if ( dest.value == "" ) dest.value = str;
   else dest.value += "," + str;
}

I use firefox for all my javascript development simply because the 
debugging tools are far superior.  Using the javascript console and DOM 
inspector can save you hours if not days.

hth
-- 
Roger Roelofs
Datacomp Appraisal Services




More information about the Javascript mailing list