Free Scripts


Creating a Navigation Menu in JavaScript: Step 2
by Christopher S.L.Heng,

JavaScript Implementation
The main function that does the hard work is as follows:

<SCRIPT LANGUAGE="JavaScript">
<!--
function menu_goto( menuform )
{
    // see http://www.thefreecountry.com/articles/javascriptmenu.html
    // for an explanation of this script and how to use it on your
    // own site
    selecteditem = menuform.newurl.selectedIndex ;
    newurl = menuform.newurl.options[ selecteditem ].value ;
    if (newurl.length != 0) {
      location.href = newurl ;
    }
}
//-->
</SCRIPT>

Put in in the <HEAD> section of your web page or, if you prefer, just before the drop down menu code (see below).

The function takes a single argument - the form object. It then proceeds to extract the selected value from the object and load the browser from that URL.

If your site is designed with frames, you will need to use "top.location.href" instead of "location.href" if you want the new document to replace your all your frames (such as when you have a URL that leaves your website). You can leave it as it is if you want the document to appear in the current frame. Alternatively, if you want it to be in another frame (sibling of the one in which the menu appears), just replace "location.href" with "parent.your_frame.location.href" where "your_frame" is the frame named "your_frame".

Now for the menu itself. Since the exact URLs for your menu and the items will differ for your site, you should modify the code given below for your situation.

<form name="menuform">
<select name="newurl" onchange="menu_goto(this.form)">
<option value="">----- Select A Page -----
<option value="index.html">Home
<option value="feedback.html">Feedback
<option value=""> ---- Other Good Sites ----
<option value="http://www.thefreecountry.com/">thefreecountry.com
</select>
<input type=submit value="Go!" onclick="menu_goto(this.form)">
</form>

Next Step: Implementing the Javascript: Step 2b >>

Copyright 2000 by Christopher S L Heng. All rights reserved.
Visit http://www.thefreecountry.com/ for more free articles, resources and tools for webmasters and software programmers.

+ Sniffers
+ Forms
+ Windows
+ 4x Browser
+ Images
+ Links
+ Frames
+ Clocks/Time
+ Misc.

Navigate

 Home
 Submit Script
 Tutorials
 Forums
 Link To Us
 Contact
 Tell A Friend
 Links
 Bookmark Us

  Sign up for our free, low volume email newsletter. Be notified when new free scripts are added.

 Email Address