[Javascript] how do you access a form element from another frame ?

David Lovering dlovering at gazos.com
Tue Dec 16 00:34:21 CST 2003


Well, the answer (unfortunately) depends a little on which browser you are
using.  For example, for IE6+ derivatives, the following should work --

var yellowTextArea =
window.parent.top.document.frameYellow.document.frm1.textArea1.value;

If you embed this inside a JavaScript routine which gets invoked on the
'onClick' event for a button, things should work OK.

Rule of thumb -- putting a "window" object in the middle of an object
declarator doesn't work, as the window is the highest level in the DOM
heirarchy (unless I'm completely whacked).  Leaving off the "document"
declarator after a frame designator doesn't generally work either, as it
tries to reference static properties/attributes of the frame itself, and not
the embedded document/form.  [I know, I know -- this doesn't make any sense.
Go cry on Bill Gate's shoulder.]  The occasions where it does work are to be
applauded, as this is how the standard says it should behave.  The "bad"
occasions are due to some wonk in Redland smoking the wrong brand of
mushrooms.

Purists will argue that the window is implicit, and makes no difference.
This is correct, insofar as the DOM references are concerned.  However, I've
found a number of situations where IE doesn't exactly follow the DOM
recommended tree, and the inclusion of the "window" prefix sometimes will
fix the problem.  I guess they figure you MIGHT want to look at elements in
another window.

Also, I've heard my ASP buddies mention that asp code doesn't necessarily
follow exactly the same OOL heirarchy as Javascript (which is a very
imperfect OOL, as far that goes), so don't expect the exact same reference
schema to work the same in both Javascript and ASP.  Sorry.

Someday everything in Java/JavaScript will be normalized to XML specs, and
we can all go take long vacations in Hawaii.

If you want to figure it out yourself, you can build a recursive object-tree
analyzer which will walk the object tree from any point you set it on,
downward until it runs out of dependent objects.  [There are several good
ones out there, although I generally use my own].

Let me know if this works, and if it doesn't, exactly where the thing goes
south.  I could probably build a sample set of code to demo the whole
kit-and-kaboodle, if someone wanted it badly enough.

-- Dave Lovering

----- Original Message ----- 
From: "DEV" <dev at qroute.net>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Monday, December 15, 2003 5:12 PM
Subject: Re: [Javascript] how do you access a form element from another
frame ?


> David,
> This is the frame set structure I have.
>
>
>  <FRAMESET rows=35,*>
>
>   <FRAME name="frameWhite" marginWidth=0 marginHeight=0 src="white.asp"
> noResize scrolling=no target="leftframe">
>
>   <FRAMESET frameborder="0" cols=319,*>
>
>    <FRAME name="frameBlue" marginWidth=0 marginHeight=0 src="blue.asp"
> noResize scrolling=auto>
>    <FRAME name="frameYellow" src="yellow.asp" scrolling=auto>
>
>   </FRAMESET>
>
>
>  </FRAMESET>
>
>
> I still cannot get to the yellow.asp's textarea from a button in the
> blue.asp.
>
> Here is the situation again;
>
> > >
> > >
> > > _______________________
> > >
> > >          ( frameGreen )
> > > _______________________
> > >                            |
> > >                            |
> > >                            |
> > > ( frameBlue )        | ( frameYellow )
> > >                            |
> > >                            |
> > >                            |
> > >
> > >
> > > frameYellow has a form named frm1 and a textarea called textArea1
> > >
> > > how does a button on the frameBlue update the value in the textArea1
> > >
> > > I tried the following, all failed.
> > >
> > > onClick="top.frameYellow.document.frm1.textArea1.value='X'"
> > >
> > > onClick="top.frameYellow.window.document.frm1.textArea1.value='X'"
> > >
> > > onClick="parent.frameYellow.document.frm1.textArea1.value='X'"
>
> What's onClick event to update yellow.asp textarea contents from the blue
?
>
>
>
>
> ----- Original Message ----- 
> From: "David Lovering" <dlovering at gazos.com>
> To: "[JavaScript List]" <javascript at LaTech.edu>
> Sent: Friday, December 05, 2003 7:27 AM
> Subject: Re: [Javascript] how do you access a form element from another
> frame ?
>
>
> > Generally, the following scheme will work across an assortment of common
> > browsers (I tested it with IE6+, and the W3C specs support it).  I'm
sure
> > some of the syntax Nazis will get on my case, but here goes:
> >
> > var greenDoc = window.parent.top.document.frames['frameGreen'].document;
> > var blueDoc = window.parent.top.document.frames['frameBlue'].document;
> > var yellowDoc =
window.parent.top.document.frames['frameYellow'].document;
> > var redDoc = window.parent.top.document.frames['frameRed'].document.
> >
> > This of course assumes that you are within the contents of one of these
> > forms (and not a remote child window), and that they are all spawned
from
> > the same top-level frameset.  Even more bizarre configurations are easy
to
> > manage with some slight tweaking.
> >
> > For example, if you were inside frameGreen, and you wanted to access
field
> > "myIndex" inside form "form1" which is contained within frameYellow,
> > something like this might work:
> >
> > myIndexValue = yellowDoc.form1.myIndex.value;
> >
> > The only caveat is that some instances may require that you use an
> > eval("stuff") instead of the core declarations I've included above.  The
> > rule for requiring or not requiring this work-around is not absolutely
> > consistant with the W3C spec (at least insofar as I've tested it with
> IE6+).
> > I'm sure someone more versed in the nuances of the "real" implementation
> of
> > the W3C specs will now step forward to enlighten me/us on the subject.
> >
> > Let me know if this works for you.
> >
> > -- Dave Lovering
> >
> > ----- Original Message ----- 
> > From: "DEV" <dev at qroute.net>
> > To: "[JavaScript List]" <javascript at LaTech.edu>
> > Sent: Thursday, December 04, 2003 9:25 PM
> > Subject: [Javascript] how do you access a form element from another
frame
> ?
> >
> >
> > > Here is the situation;
> > >
> > >
> > > _______________________
> > >
> > >          ( frameGreen )
> > > _______________________
> > >                            |
> > >                            |
> > >                            |
> > > ( frameBlue )        | ( frameYellow )
> > >                            |
> > >                            |
> > >                            |
> > > _______________________
> > >
> > >          ( frameRed )
> > > _______________________
> > >
> > >
> > > frameYellow has a form named frm1 and a textarea called textArea1
> > >
> > > how does a button on the frameBlue update the value in the textArea1
> > >
> > > I tried the following, all failed.
> > >
> > > onClick="top.frameYellow.document.frm1.textArea1.value='X'"
> > >
> > > onClick="top.frameYellow.window.document.frm1.textArea1.value='X'"
> > >
> > > onClick="parent.frameYellow.document.frm1.textArea1.value='X'"
> > >
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > Javascript mailing list
> > > Javascript at LaTech.edu
> > > https://lists.LaTech.edu/mailman/listinfo/javascript
> > >
> >
> >
> > _______________________________________________
> > Javascript mailing list
> > Javascript at LaTech.edu
> > https://lists.LaTech.edu/mailman/listinfo/javascript
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>





More information about the Javascript mailing list