Creating a Forms-Based Navigation Menu with JavascriptForms are used throughout the Web. You can select items from a list, click on "radio buttons," and enter text into fields. If you've used a search site on the Web, such as Lycos, Yahoo, or AltaVista, you've most likely used a form. In this primer, I'll explain how to use a select list to create a forms-based navigation menu. Forms:Forms are created using the <FORM> tag. Between the <FORM> and </FORM> tags, you can specify the type of form using the tags <INPUT>, <SELECT>, <OPTION>, <TEXTAREA> and <LABEL>. These tags, and their accompanying attributes, allow you to specify the appearance and behavior of the forms you create. To read more about forms, please visit the forms links page. Select Lists:The <SELECT> tag allows you to create a pull-down or pop-up menu. (Whether the form appears as one or the other depends on where the form is located relative to the top or bottom of your browser window. If it's far enough above the bottom of the window, the list will drop down; otherwise, it will pop up.) A select list contains a set of choices.Example: A Simple Select List <FORM NAME="Color_List" ACTION="" METHOD=POST><SELECT> <OPTION Value="I did not fill out this form" SELECTED>My Favorite Color is: <OPTION VALUE="green">Green <OPTION VALUE="blue">Blue <OPTION VALUE="red">Red <OPTION VALUE="yellow">Yellow <OPTION VALUE="Orange">Orange <OPTION VALUE="Purple">Purple <OPTION VALUE="None of the Above">None of the Above <OPTION VALUE="I am Color Blind">I am Color Blind </SELECT> </FORM> This form doesn't actually do anything, since it needs to be connected to either a CGI-script (a script that runs on the Web server) or a javascript (a script which runs on your browser). But this form does illustrate the key elements of a pull-down menu:
Turning a Select List into a Navigation MenuTo turn a select list into a navigation menu, you need to provide a a list of URLs and a script that will run when an item in the list is selected. I use the following script because it allows me to target the URL to a particular frame. You can target the link to a frame on the page you are currently viewing, or you can target it to a new window. If you target it to a new window, you can control the size and appearance of that window. (I obtained this script from the fine folks at the NetObjects Workbench site and I greatly appreciate their thoughtfulness in providing it.)
<HTML>
<HEAD>
<TITLE>Forms Navigation Menu Example</TITLE>
<script language="JavaScript">
<!-- Hide the script from old browsers --
function surfto(form) {
var NewWindow=window.open('','popup','menubar=1,toolbar=1,location=1,directories=1,scrollbars=1,resizable=1,status=1,width=640,height=480');
NewWindow.focus();
var myindex=form.dest.selectedIndex
window.open(form.dest.options[myindex].value,target="popup");
}
// -->
</SCRIPT>
</HEAD>
This script, when activated by the link, opens the specified html file in a window named "popup". If the window has already been opened, it will bring it to the front of the desktop (in front of every other window on your computer's desktop) regardless of whether it has been minimized or just hidden behind other windows. If this script is used on a framed page that has a frame named popup, it will open the file in the popup frame and ignore the parts of the script that specify the size and appearance of the window (see below). To control the size and appearance of the window, you can set the following values in the script:
This script, when activated by the form (see the form code below), opens the link in a window or frame defined by the target (in this case, a new window called "popup"). You can change the window or frame that is targeted by simply changing the name. To link this script to a navigation menu, you need to modify your <FORM> code as follows:
<FORM NAME="Links" ACTION="" METHOD=post>
<SELECT ID="Links_List" NAME="dest" onchange= "surfto(this.form)">
<OPTION VALUE="primer_formnav.cfm#menu" SELECTED>Useful Links
<OPTION VALUE="links_html.cfm">HTML Tutorials and Resources
<OPTION VALUE="links_forms.cfm">HTML Forms
<OPTION VALUE="links_graphics.cfm">Web Graphics
<OPTION VALUE="links_javascript.cfm">JavaScript Tutorials and Resources
<OPTION VALUE="links_maps.cfm">Client-Side Image Maps
</SELECT>
<INPUT TYPE=button NAME="Menu_Button" VALUE="View"
ID="Menu_Button" onclick = "surfto(this.form)">
</FORM>
The key elements in the form code are:
If you'd like, you can try the navigation menu:
|
Copyright © 1993-2009 Colorado State University and/or this site's authors, developers, and contributors. Some material displayed on this site is used with permission.