Advanced HTML Guide

Java Menu Source Code

This is a menu which will open the selected page in the same frame as where the menu is placed.


This page is here to help you make a Java menu, ie you copy the code out of this page and stick it into your own page, easy. I was trying to make a Java menu some time ago and I`d found 3 pieces of code written by other people, one of them only worked in Explorer 3 and not Netscape the second only worked if you had a frame with the menu in and made the page appear in the other frame, the last one was a Java Applet that made the page appear on the same page but it got rid of any frames that were there. I cunningly combined the first two to make this.


The following is placed in the HEAD part of the page

<SCRIPT LANGUAGE="JavaScript">
<!--
function openDir (form) {
        if (form.directory.selectedIndex == 0)
                parent.window.location.href="advancedhtml.htm"
        if (form.directory.selectedIndex == 1)
                parent.window.location.href="frame.htm"
        if (form.directory.selectedIndex == 2)
                parent.window.location.href="faq.htm"
        if (form.directory.selectedIndex == 3)
                parent.window.location.href="/"
}
// end of script -->
</SCRIPT>

The form is placed anywhere on the page between the BODY tags.

<FORM NAME="myform">
  <SELECT NAME="directory">
     <OPTION SELECTED="SELECTED">Advanced HTML Page</OPTION>
     <OPTION>Frames Page</OPTION>
     <OPTION>HTML FAQ</OPTION>
     <OPTION>Back To Start Page</OPTION>
  </SELECT>
  <INPUT TYPE="button" NAME="button" VALUE=" Go " ONCLICK="openDir(this.form)">
</FORM>

When you choose an option from the list the page will appear in the same frame as the menu. You could modify it to make the page go into a different frame as I have do on The Frames Page. It consists of 2 parts, the actual Java code is in the Head part of the page and that is where all the names of the pages are typed. The second part of the menu is a simple form where you put the names of the pages that you want to appear. When you choose something from the list the Java code relates this to the right page and opens it. The advantage of this is that your page may be called `asr.htm` but in the menu it can be seen as `Annual Spending Report`.

Menus In Frames

If you go to the Frames page you can see a JavaScript menu like this one being used in a frame and loading the page into the main frame. This can be done by using the code above simply changing the "window.location.href" to "parent.main.location" where "main" is the name of the frame that you want the page loaded into.


Netscape Users Netscape is Case Sensitive when it comes to JavaScript which for example means that openDir is totally different to opendir.


The only problem is that if you use a browser that doesn`t support Java then the menu will still appear but it won`t do anything. This menu has been tested in Internet Explorer 3 & 4 and Netscape Navigator 3 & 4 and works perfectly in all of them.


  The  
Advanced  HTML
Site
Privacy Policy
Advanced HTML Home
Copyright © 1997 - 2024
Hosted by IONOS