Java Reference
In-Depth Information
The top Property
Using the parent property can get a little tedious when you want to access the very top window from
a frame quite low down in the hierarchy of frames and window objects. An alternative is the window
object's top property. This returns a reference to the window object of the very top window in a frame
hierarchy. In the current example, this is top window.
For instance, in the example you just saw, this code:
window.parent.parent.frameMenu.document.myForm.myControl.value;
could be written like this:
window.top.frameMenu.document.myForm.myControl.value;
Although, because the window is a global object, you could shorten that to just this:
top.frameMenu.document.myForm.myControl.value;
So when should you use top rather than parent, or vice versa?
Both properties have advantages and disadvantages. The parent property enables you to specify
window objects relative to the current window. The window above this window is window.parent, its
parent is window.parent.parent, and so on. The top property is much more generic; top is always
the very top window regardless of the frameset layout being used. There will always be a top, but
there's not necessarily going to always be a parent.parent. If you put all your global functions and
variables that you want accessible from any page in the frameset in the very top window, window.top will
always be valid regardless of changes to framesets beneath it, whereas the parent property is dependent
on the frameset structure above it. However, if someone else loads your web site inside a frameset page
of his own, then suddenly the top window is not yours but his, and window.top is no longer valid. You
can't win, or can you?
One trick is to check to see whether the top window contains your page; if it doesn't, reload the top
page again and specify that your top page is the one to be loaded. For example, check to see that the
fi le name of the top page actually matches the name you expect. The window.top.location.href
will give you the name and path — if they don't match what you want, use window.top.location
.replace(“myPagename.htm”) to load the correct top page. However, as you'll see later, this will
cause problems if someone else is loading your page into a frameset they have created — this is where
something called the same-origin policy applies. More on this later in the chapter.
Try It Out Scripting Frames
Let's put all you've learned about frames and scripting into an example based on the frameset you last
looked at in ch08_examp2.htm. You're going to be reusing a lot of the pages and code from the previous
example in this chapter.
The fi rst page you're creating is the top window page. The highlighted lines of code show changes
made to ch08_examp2.htm.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Frameset//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
Search WWH ::




Custom Search