Frames And JavaScript - Part II


© Muhammad Ali Shah
Articles in this Topic    Discussions in this Topic

  • Level: Intermediate
  • Skills Required: HTML Frames and Basic JavaScript


Scenario:

In our last article, we saw how to create frames using HTML and how to define A HREF links that change the contents of the same frame as well as in the other siblings frames. Today we will see how to refer to windows and frames in JavaScript. We will create a simple JavaScript driven page that has two frames; a button in one frame changes the background color of the other.


Frames Array:

Like links and images, when you define frames in HTML the web browser automatically creates corresponding objects that can be referred via JavaScript. Please read the last sentence once again. It's important.

Taking the example from the last article, suppose you have defined an HTML page (index.html) with frames as shown below:

<html>
<head>
<title>Frames Example</title>
</head>

<frameset cols="30%, 70%">
   <frame name="menu" src="menu.html">
   <frame name="content" src="home.html">
</frameset>
</html>

When they read this code, all JavaScript capable web browsers will create an array of frames whose elements can be accessed by frames[i] .elementName; where i is the index of the array and elementName is the name of the object that you want to access. You can also use the name of the frame (menu or content in our example) in place of the array elements. A window can communicate with its parent window using top keyword. For example, if you want to change the background color of the second frame from the code in the first one, you can write

top.frames[1] .document.bgColor = "blue"
or
top.content.document.bgColor = "blue"

Remember, in JavaScript the array index begins with zero. A detailed example will clarify things.


Exploring Further:

Now we will do the example that we promissed in the beginning. Consider we have three pages: index.html (like the one defined above), home.html (the page whose background color will be changed) and menu.html (the page which will have a button to change the color of the second page). As described earlier, the role of index.html is just to define the two windows and load menu.html and home.html into them.

The code for index.html has already been given above. You can put whatever you like in home.html. The code for menu.html is given below. Study it carefully.

<html>
<head>
<script language="JavaScript">
function changeColor()
{
   var color;
   var index = document.example.color.selectedIndex;
   color = document.example.color.options[index] .value;
   top.frames[1].document.bgColor = color;
}
</script>
</head>

<body>
<P>This is menu.html.</p>
<p><form name="example">
<select name="color">
   option value="#FFFFFF">White
   option value="#D00000">Red
   option value="#00A000">Green
</select>
<input type="button" value="Change Color" onClick="changeColor()">
</form></p>
</body>
</html>


Last Words:

This covers most of the concepts regarding frames and frame arrays. Do you have any questions? Or if you are stuck up somewhere, feel free to ask in Discussions Area. I repeat my statement:

Figure
       

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo