July 29, 2000 - Sharing Functions between Frames | WebReference

July 29, 2000 - Sharing Functions between Frames

Yehuda Shiran July 29, 2000
Sharing Functions between Frames
Tips: July 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

When you work with frames, there is always the question how to share functions between frames. It is much simpler than you think. You can access any function in any frame by specifying its full path name, starting from the top frameset. Let's take a frameset with two frames, upper and lower. Here is the frameset definition:

<HTML>
<HEAD>
<TITLE>Demonstrate Function Calls in Frames</TITLE>
</HEAD>
<FRAMESET ROWS="50, 50" FRAMEBORDER="yes" FRAMESPACING="1"> 
  <FRAME SRC="000729c.html" NAME="upper" SCROLLING="no" MARGINHEIGHT="1" MARGINWIDTH="1">
  <FRAME SRC="000729b.html" NAME="lower" SCROLLING="no" MARGINHEIGHT="1" MARGINWIDTH="1">
</FRAMESET>
</HTML>

The upper frame is defined in 000729c.html:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function hello() {
  alert("Hello from the Upper Frame");
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Upper Frame</H1>
</BODY>
</HTML>

And the lower frame is in 000729b.html:

<HTML>
<HEAD>
</HEAD>
<BODY>
<H1>Lower Frame</H1>
<A HREF="javascript:top.upper.hello()">Call hello function from Upper Frame</A>
</BODY>
</HTML>

Notice how we reference hello() from the upper frame: top.upper.hello(). For more on frames, go to Column 37, JavaScript and Frames, Part II - The Famous Memory Game.