Welcome to Australian PC User Magazine Offline CD-ROM
Software - What's on our latest cover CD. What's New - Like . . "What's new?" Net Guide - Our comprehensive guide to using the Net Web Workshop - Tutorials for Web developers Net Sites - Check out the best on the Web About Us - Find out about PC User Magazine and PC User Net nineMSN
clear.gif (118 bytes)
Web Workshop Contents

Home
Search
Help!

Framing Web Site menus - Using JavaScript with Frames

Helen Bradley and John Hilvert look at controlling a site that uses frames with JavaScript code, and also how you can adapt last month's rollover menu to run in a small frame at the top of your page.

 

We've provided all the files you need for this project on this month's cover CD-ROM.

From the CD (see 'Files on cover CD' on how to obtain them) load example*.htm (*depending on which example you wish to use) into your browser using File, Open and note which page displays on your browser. If you have more than one browser on your computer, open it in the others and notice how the page which appears changes according to the browser and version you're using.

To view or use the source code for each file, you can use any HTML editor, text editor and most Web browsers.

The HTML source in this project has been written so that you can use it for a number of applications. It displays one of a range of three pages depending on what browser your user is using, and is easily expandable beyond this. The source has been designed so that you can easily adapt it to your own programs.

 

Frame basics
Before you begin managing frames using JavaScript you may want to brush up on creating a page using frames. Example 1 is a simple page split vertically into two frames. To create it, first create the pages from the source in Source Box #1 (if you completed last month's project you'll already have the files next.htm, back.htm, contact.htm and photos.htm).

Load the file example1.htm into your browser using File, Open. When the file is loaded, select a menu option from the left of the screen and watch as different pages appear in the frame on the right (see Figure 1d).

The source that creates the frames is in example1.htm. The <FRAMESET>... </FRAMESET> tags replace the BODY tags and create two columns: the first one called 'menu' is 90 pixels wide and the second one, called 'main' is the width of the remainder of the browser window. The file menu1.htm is loaded into the left frame and the file instruct.htm is loaded into the right frame:

<FRAMESET COLS=90,*>
<FRAME NAME="menu" SRC="menu1.htm" MARGINHEIGHT=0 MARGINWIDTH=0 SCROLLING=NO NORESIZE>
<FRAME NAME="main" SRC="instruct.htm" MARGINHEIGHT=0 MARGINWIDTH=0 SCROLLING=AUTO>
</FRAMESET>

Selecting a menu option from the left frame replaces the page in the right frame with another page. For example, if you select 'Back', the page back.htm is loaded into the frame called 'main' by the use of the TARGET attribute:

<A HREF="back.htm" TARGET=main>Back</A><P>

If you select the menu option 'Home' the frames disappear entirely as the file main1.htm is loaded into the entire window by the use of the attribute TARGET=_top:

<A HREF="main1.htm" TARGET=_top>Home</A> (exits frames)<P>

A simple JavaScript example

Adapting this first example to JavaScript involves creating two simple JavaScript functions. The first function loadMainFrame takes a URL from the calling statement and opens this page in the frame called main:

function loadMainFrame(pageToLoad) {
    parent.main.location.href=pageToLoad;
    }

Frames are contained within the window that contains the frameset itself, they are considered to be 'child' windows of this parent window. There are a number of ways you can refer to a frame window in JavaScript. The one used here is to refer to the frame using the name given to it in the frameset tag that created it using the syntax parent.framename.

Another way of referencing the frame is to use the frames[] array where the frames are indexed 0, 1, 2 etc in order of creation. In this example, the frame called 'main' was the second one created, so its index is 1 and it can be referenced as parent.frames[1].

The function exitFrames shows how frames can be removed by loading a page into the parent window, which is the window that contains the frameset. This is equivalent to using TARGET=_top as was used in Example 1. Because this function will only be used in one instance where the user selects the Home button, there is no need to pass a URL to this function and it can contain the actual page name to be loaded:

function exitFrames() {
    parent.location.href="main2.htm"
    }

To create this example you'll need some of the files from Example 1 (next.htm, back.htm, contact.htm and photos.htm) and the additional files listed in Source Box #2. Create these files and load the file example2.htm into your browser.

 

Adapting the rollover menu
Adapting the rollover menu from last month's tutorial is quite simple. Open the file example5.htm from the April cover CD-ROM and save it as menu.htm. Then add the two new functions loadMainFrame and exitFrames to the script in the head of the document.

You'll also need to alter each of the button's link details. Each button will now be linked to a dummy jump link "#" and will use the onClick event handler to call one of the new functions when the button is selected.

You'll also need to create the file mainpage.htm which contains the source for the frames. In this case the page is split into two frames horizontally across the browser window rather than vertically down it because the buttons are in a single table row which stretches across the screen. The top frame has been sized to suit the size of the menu bar images.

 

Browser compatibility
The source code examples in this tutorial work fine with Netscape 3 and higher and Internet Explorer 4 but won't work in Internet Explorer 3.0. If you have IE3, you'll find that the routines don't crash the program it just doesn't support the features used here -- a good example of a program 'degrading gracefully'.

A sensible approach to take is to team this menu with the browser checker from March's tutorial and send users with a JavaScript browser to your enhanced page containing the frames and menu and leave other users on the browser checking page with a plainer, non-JavaScript menu.

 

Web resources
Netscape's authoring guide: http://home.netscape.com/eng/mozilla/Gold/handbook/javascript/index.html

Yahoo's JavaScript Index: http://www.yahoo.com/Computers_and_Internet/Programming_Languages/JavaScript/

Books
The JavaScript Sourcebook
, by Gordon McComb, would be a valuable addition to your library.
Published by John Wiley and Sons.

corner.gif (190 bytes)

clear.gif (118 bytes)
Files on cover CD
On this month's (May '98) CD are the files:
For Example 1: example1.htm, main1.htm, menu1.htm, instruct.htm, home.htm, next.htm, back.htm, photos.htm, contact.htm.

For Example 2: example2.htm, main2.htm, menu2.htm.

For Example 3: mainpage.htm, menu.htm, home.htm, img0on.jpg-img4on.jpg, img0off.jpg-img4off.jpg.

If you have problems viewing any of these files with your current browser, try installing either Netscape Navigator 4 or Internet Explorer 4 from the cover disc and view it using one of these browsers.

You will find these files in the Interact\menus folder on the CD.

 

 

 

 

 

 

 

 

FRAME1s.gif (1277 bytes)
Figure 1d: Using frames you can create a static menu in one frame and load the information pages from your site into the larger frame.

FRAME2s.gif (1196 bytes)
Figure 2d: With some minor changes, the rollover menu from last month's tutorial can be placed in a frame and manipulated using JavaScript.

 

note.gif (244 bytes)Source Box 1

 

note.gif (244 bytes)Source Box 2

 

note.gif (244 bytes)Source Box 3

 

 

 

 

note.gif (244 bytes)Help! Why won't it work?

toppage.gif (1757 bytes)copyrite.gif (1355 bytes)