Apache web server - 362Part IIIDocument Objects ReferenceReferencing framesThe purpose of an

362Part IIIDocument Objects ReferenceReferencing framesThe purpose of an object reference is to help JavaScript locate the desired object in theobject model currently held in memory. A reference is a road map for the browser to follow, so that it can track down, say, the value of a particular text field in a particular document. Therefore, when you construct a reference, think about where the script appears in theobject model and how the reference can help the browser determine where it should go tofind the distant object. In a two-generation scenario, such as the one shown in Figure 16-2, three intergenerational references are possible: .Parent-to-child .Child-to-parent .Child-to-childAssuming that you need to access an object, function, or variable in the relative s frame, thefollowing are the corresponding reference structures: frameName.objFuncVarName; parent.objFuncVarName; parent.frameName.objFuncVarName. The rule is this: Whenever a reference must point to another frame, begin the reference withthe windowobject that the two destinations have in common. To demonstrate that rule onthe complex model in Figure 16-3, if the left-hand child frame s document needs to referencethe document at the bottom right of the map, the reference structure istop.frameName.frameName.document. … Follow the map from the top windowobject down through two frames to the final document. JavaScript has to take this route, so your reference must help it along. Top versus parentAfter seeing the previous object maps and reference examples, you may be wondering, Whynot use topas the leading object in all trans-frame references? From an object model point ofview, you ll have no problem doing that: A parent in a two-generation scenario is also the topwindow. What you can t count on, however, is your framesetting document always being thetopwindow object in someone s browser. Take the instance where a Web site loads otherWeb sites into one of its frames. At that instant, the topwindow object belongs to someoneelse. If you always specify topin references intended just for your parent window, your refer- ences won t work and will probably lead to script errors for the user. My advice, then, is touse parentin references whenever you mean one generation above the current document. Preventing framingYou can use your knowledge of topand parentreferences to prevent your pages from beingdisplayed inside another Web site s frameset. Your top-level document must check whether itis loaded into its own top or parent window. When a document is in its own top window, a ref- erence to the topproperty of the current window is equal to a reference to the current win- dow (the windowsynonym selfseems most grammatically fitting here). If the two values arenot equal, you can script your document to reload itself as a top-level document. When it iscritical that your document be a top-level document, include the script in Listing 16-1 in theHead portion of your document:

Leave a Reply