Medical web site - 380Part IIIDocument Objects ReferenceTo use the framesproperty to
380Part IIIDocument Objects ReferenceTo use the framesproperty to communicate from one frame to another, it should be part of areference that begins with the parentor topproperty. This lets JavaScript make the properjourney through the hierarchy of all currently loaded objects to reach the desired object. Tofind out how many frames are currently active in a window, use this expression: parent.frames.lengthThis expression returns a number indicating how many frames the parent window defines. This value does not, however, count further nested frames, should a third generation of framebe defined in the environment. In other words, no single property exists that you can use todetermine the total number of frames in the browser window if multiple generations offrames are present. The browser stores information about all visible frames in a numbered (indexed) array, withthe first frame (that is, the topmost tag defined in the framesetting document) asnumber 0: parent.frames[0] Therefore, if the window shows three frames (whose indexes are frames[0], frames[1], andframes[2], respectively), the reference for retrieving the titleproperty of the document inthe second frame isparent.frames[1].document.titleThis reference is a road map that starts at the parent window and extends to the secondframe s document and its titleproperty. Other than the number of frames defined in a par- ent window and each frame s name (top.frames[i].name), no other values from the framedefinitions are directly available from the frame object via scripting until you get to IE4 andNN6/W3C (see the frameelement object later in this chapter). In these browsers, individualframeelement objects have several properties that reveal tag attributes. Using index values for frame references is not always the safest tactic, however, becauseyourframeset design may change over time, in which case the index values will also change. Instead, you should take advantage of the nameattribute of the tag, and assign aunique, descriptive name to each frame. A value you assign to the nameattribute is also thename that you use for targetattributes of links to force a linked page to load in a frameother than the one containing the link. You can use a frame s name as an alternative to theindexed reference. For example, in Listing 16-7, two frames are assigned distinctive names. Toaccess the title of a document in the JustAKid2frame, the complete object reference isparent.JustAKid2.document.titlewith the frame name (case-sensitive) substituting for the frames[1]array reference. Or, inkeeping with JavaScript flexibility, you can use the object name in the array index position: parent.frames[ JustAKid2 ].document.titleThe supreme advantage to using frame names in references is that no matter how the frame- set structure may change over time, a reference to a named frame will always find that frame, although its index value (that is, position in the frameset) may change. ExampleListings 16-7 and 16-8 demonstrate how JavaScript treats values of frame references fromobjects inside a frame. The same document is loaded into each frame. A script in that docu- ment extracts information about the current frame and the entire frameset. Figure 16-4 showsthe results after loading the HTML document in Listing 16-7. windowObject.frames