364Part IIIDocument Objects ReferenceSwitching from frames to (Web server setup) framelessSome

364Part IIIDocument Objects ReferenceSwitching from frames to framelessSome sites load themselves in a frameset by default and offer users the option of getting ridof the frames. Only IE4+ and W3C browsers let you modify a frameset s colsor rowsproper- ties on the fly to simulate adding or removing frames from the current view (see the frame- setelement object later in this chapter). In other browsers, you cannot dynamically changethe makeup of a frameset after it has loaded, but you can load the content page of the frame- set into the main window. Simply include a button or link whose action loads that documentinto the topwindow object: top.location.href = mainBody.html ; A switch back to the frame version entails nothing more complicated than loading the frame- setting document. Inheritance versus containmentScripters who have experience in object-oriented programming environments probablyexpect frames to inherit properties, methods, functions, and variables defined in a parentobject. That s not the case with scriptable browsers. You can, however, still access those par- ent items when you make a call to the item with a complete reference to the parent. For exam- ple, if you want to define a deferred function in the framesetting parent document that allframes can share, the scripts in the frames refer to that function with this reference: parent.myFunc() You can pass arguments to such functions and expect returned values. Frame synchronizationA pesky problem for some scripters plans is that including immediate scripts in the frameset- ting document is dangerous. Such scripts tend to rely on the presence of documents in theframes being created by this framesetting document. But if the frames have not yet been cre- ated and their documents have not yet loaded, the immediate scripts will likely crash andburn. One way to guard against this problem is to trigger all such scripts from the frameset sonloadevent handler. In theory, this handler won t trigger until all documents have success- fully loaded into the child frames defined by the frameset. Unfortunately, IE4+ for Windowshas a nasty bug that fires the onloadevent handler in the frameset even if the loading hasbeen interrupted by the browser s Stop button or a press of the Esc key. At the same time, becareful with onloadevent handlers in the documents going into a frameset s frames. If one ofthose scripts relies on the presence of a document in another frame (one of its brothers orsisters), you re doomed to eventual failure. Anything coming from a slow network or serverto a slow modem can get in the way of other documents loading into frames in the idealorder. One way to work around these problems is to create a Boolean variable in the parent docu- ment to act as a flag for the successful loading of subsidiary frames. When a document loadsinto a frame, its onloadevent handler can set that flag to trueto indicate that the documenthas loaded. Any script that relies on a page being loaded should use an ifconstruction totest the value of that flag before proceeding.

Leave a Reply