Java Reference
In-Depth Information
When one of these pages is loaded, its name is put into the pagesVisited array defi ned in ch08_
examp2.htm by the window object's onload event handler's being connected to the addPage() function
that you also created in ch08_examp2.htm. You connect the code to the event handler in the <body />
element of the page as follows:
<body onload=”window.parent.addPage(window.location.href)”>
Recall that all the functions you declare in a page are contained, like everything else in a page, inside
the window object for that page; because the window object is the global object, you don't need to prefi x
the name of your variables or functions with window .
However, this time the function is not in the current page, but in the ch08_examp2.htm page. The
window containing this page is the parent window to the window containing the current page. You
need, therefore, to refer to the parent frame's window object using the window object's parent property.
The code window.parent gives you a reference to the window object of ch08_examp2.htm. With this
reference, you can now access the variables and functions contained in ch08_examp2.htm. Having stated
which window object you are referencing, you just add the name of the function you are calling, in this
instance the addPage() function. You pass this function the location.href string, which contains
the full path and fi le name of the page, as the value for its one parameter.
As you saw earlier, the button on the page has its onclick event handler connected to a function called
btnShowVisited_onclick() . This is defi ned in the head of the page.
function btnShowVisited_onclick()
{
document.form1.txtaPagesVisited.value =
window.parent.returnPagesVisited();
}
In this function, you call the parent window object's returnPagesVisited() function, which, as you
saw earlier, returns a string containing a list of pages visited. The value property of the textarea object
is set to this text.
That completes your look at the code in the frame pages, and as you can see, there's not much of it because
you have placed all the general functions in the frameset page. Not only does this code reuse make for
less typing, but it also means that all your functions are in one place. If there is a bug in a function, fi xing
the bug for one page also fi xes it for all other pages that use the function. Of course, it only makes sense to
put general functions in one place; functions that are specifi c to a page and are never used again outside
it are best kept in that page.
Code Access Between Frames
You've just seen how a child window can access its parent window's variables and functions, but how
can frames inside a frameset access each other?
You saw a simple example earlier in this chapter, so this time let's look at a much more complex example.
When created, your page will look like the one shown in Figure 8-7.
Search WWH ::




Custom Search