background
|
|
bonsai
The Element
A small area for Bonsai related material.
More icon

bonsai
The Element
A small area for Bonsai related material.
More icon
|
|
Up Image
Navigation
Search this Site
Enter your search terms

Site Breadcrumb - You are here
|
Thau's JavaScript Tutorial
Lesson 3

by Thau!

Page 11 — Getting Framed

All the remaining properties of windows that we'll see in this lesson relate to frames and how to use them with JavaScript. If you don't know how to use frames, check out Jillo's Frames Are a Picnic. Read Jillo's wisdom, practice building a few framesets, and then come back. After I show you how to play with frames in JavaScript, I'll give you your homework for the day.

In JavaScript, frames are treated just like windows. Just as you can tweak the contents of one window using JavaScript inside a different window, you can change the contents of one frame using JavaScript in another frame.

Here's a straightforward example of using frames with JavaScript. I'll break it down line by line.

The first thing we need to look at is the frameset. Here it is:

<frameset rows="25%,*">
<frame src="frames_example_controls.html" name="control_frame">
<frame src="blank.html" name="target_frame">
</frameset>

This is just like any ordinary frameset. The important thing to note and remember is that the frames in the frameset are named. The first frame, called control_frame, holds the HTML page that contains the JavaScript. The second frame, as you can see from the src="blank.html", contains nothing.

The second thing to look at is the contents of the control_frame. To keep things simple, it only has one interesting line:

<a href="#" onClick="top.target_frame.document.writeln('Monkey do!<br>');">Monkey see</a>

This line tries to write the words "Monkey do!<br>" into the bottom frame. But before JavaScript can write to the frame, it has to know what frame we're talking about. The expression top.target_frame, which you'll see in the onClick, tells JavaScript to look at the top-most window, which is the browser itself, and find the thing called target_frame inside that window. Because we called the bottom frame "target_frame" when defining the frameset, JavaScript will know that target_frame means the bottom frame.

Once we've told JavaScript which frame we're talking about, we can treat the frame just like a window. To write the word "howdy" to a window called greeting_window, we'd do this:

greeting_window.document.writeln("howdy");

This tells JavaScript to find the window named greeting_window, then find the document of that window, and then write the word "howdy" into that document. Yes, if you haven't figured it out yet, writeln() is a method of the document object, which is why we've had to write document.writeln('blah blah) instead of just writeln('blah blah').

Because frames are treated just like windows, we can do a similar thing:

top.target_frame.document.writeln("Monkey do!");

Now, let's learn more about windows and frames.

next page»