Home > Articles > Programming > JavaScript

JavaScript for Navigation

  • Print
  • + Share This
This chapter is from the book

In This Chapter

  • Project I: JavaScript and Pull-Down Menus—Getting to Where You Want to Go!

  • Project II: Using Multiple Pull-Down Menus for Navigation

  • Project III: Using JavaScript on a Log-in Page

  • Project IV: Using CSS/DHTML with JavaScript for Navigation

  • Recap

  • Advanced Projects

So far, you have done a great job adding some new functionality and pizzazz to Shelley Biotech's homepage. Now that your boss has seen how great JavaScript can be, another idea has popped into his head. The powers that be would love to be able to add a pull-down menu to the secondary navigation on the site that would let the user jump quickly to certain pages that are several levels down in the site with only a single click. Of course, like most of your projects, they need it done ASAP, so it's off we go on another "learn while you go" assignment.

After meeting with one of the designers, you feel that you have worked out a good place to put the pull-down menu on the homepage (see Figure 3-1). Now you just need to get the links that they want you to populate the pull-down with. An email to your boss takes care of that easily enough, and you're soon ready to go.

03fig01.jpgFigure 3-1. Pull-down menu added to the secondary navigation.


Project I: JavaScript and Pull-Down Menus—Getting to Where You Want to Go!

The first thing we need to do is insert the HTML code into the secondary navigation that will give us our pull-down menu and Go button. Here is the code that we will use.

<select name="PullDown">
     <option value=" ">Get There Quick
     <option value=" ">What's New
     <option value=" ">Featured Product
     <option value=" ">Press Releases
     <option value=" ">Company Store
</select>
<input type="button" name="Go" value="Go">

Because we will be inserting the pull-down into a preexisting design, we will place the <FORM> tag, which will be named NavForm, at the beginning and end of the HTML body instead of directly around the form element to prevent any unwanted line breaks or spaces. You will also notice that the value properties of the options are left blank; we will fill those in later when we insert the event handler needed to run the script.

Creating the Navigation Function

As the pull-down navigation is going to be on every page in the site, we place the function that will run it in our external JavaScript file right below the Product Banner Randomizer functions, like so.

setTimeout("bannerChanger()", 20000);


// Pull-Down Menu Navigation Function
function PageChanger(page) {
   . . .
}

The function itself will be fairly simple and will consist of only two parts. Notice that we pass a value into the function that is assigned to the variable page. This value will be the location of the page that the user has selected from the pull-down menu. In the first line, we use that value to change the location property of the DOCUMENT object; this reloads the browser window with the new Web page.

 // Pull-Down Menu Navigation Function
function PageChanger(page) {
     document.location= page;
     . . .
}

Next, we add a line that serves a housekeeping function more than anything else. When the function is run, this line resets the pull-down menu to the first choice in the menu. This is helpful if the user uses the Back button to return to this page. If we didn't put this line in, on some browsers the last selection the user made in the pull-down would still be visible.

// Pull-Down Menu Navigation Function
function PageChanger(page) {
     document.location= page;
     document.NavForm.PullDown.options[0].selected=true;
}

Inserting the Event Handler

Now that we have created our function, we need to insert the event handler into our HTML to call the function. Before we do that, let's insert the value properties into the pull-down menu's options. As stated earlier, the value that we pass into our function is the URL for the page we want to send the user to. This value comes from the VALUE attribute of our menu options. Therefore, for the value of each option, we need to put the location of the page for that selection.

<select name="PullDown">
     <option value="">Get There Quick
     <option value="whatsnew.html">What's New
     <option value="featured_product.html">Featured Product
     <option value="press.html">Press Releases
     <option value="store.html">Company Store
</select>

For the first menu option, which is just a title put there for aesthetic purposes, we don't put in a value because we don't want that option to send the user anywhere. Once the user selects the desired menu option, he or she needs to click on the Go button to get there. To trigger this, we need to insert an onClick handler into the HTML for the Go button.

<select name="PullDown">
     <option value="">Get There Quick
     <option value="whatsnew.html">What's New
     <option value="featured_product.html">Featured Product
     <option value="press.html">Press Releases
     <option value="store.html">Company Store
</select>

<input type="button" name="Go" value="Go" onClick="PageChanger(document.NavForm.PullDown
graphics/ccc.gif.options [NavForm.PullDown.selectedIndex].value)">
   

We do two things in the event handler: We call the PageChanger() function and we pass a value to it. The value that we pass is the value of the option that the user has chosen from the pull-down menu. In the functions we have created so far, the value that we have passed into the function has been a simple string. In this event handler, we are trying something new: We are referencing the pull-down menu object that has been selected and accessing its value. This value is then passed into the function and used to send the user to the page he or she wants.

Our script is now functional and ready to go. However, because of the Go button we added to the page, the categories to the right of the pull-down menu have been forced to take up two lines (Figure 3-2), which unfortunately isn't acceptable, so we need to find another way to do it. Luckily for us, there is another way we can power our script that will not only take care of this problem, but will make navigating with the pull-down menu even faster.

03fig02.jpgFigure 3-2. Page with pull-down menu and Go button.


Using onChange for Instant Gratification

To get the page layout back to its original state, we need to get rid of the Go button; however, at the moment, it contains the event handler that triggers our script. What we need is a handler that we can put into the pull-down menu itself to run our script for us. Fortunately, just such a handler exists, the onChange event handler. This handler looks for a change in the state of the object that it is contained within, and when it finds one, it triggers.

By inserting it in our pull-down menu, we can have it activate our function when the user changes the menu option from the default option that is loaded with the page. Let's see how this changes our HTML code.

<select name="PullDown" onChange="PageChanger(this.options[this.selectedIndex] .value)">
     <option value=" ">Get There Quick
     <option value="whatsnew.html">What's New
     <option value="featured_product.html">Featured Product
     <option value="press.html">Press Releases
     <option value="store.html">Company Store
</select>

First, notice that we have removed the Go button, as it is no longer needed. Second, the onChange event handler has been added to the pull-down menu. The value we pass to the function is the same: the value of the option the user has selected. However, we call that value differently than the way we called it before. Because the event handler is in the form element that we wish to get the value from, we can use the following method.

this.options[this.selectedIndex].value

Instead of calling the specific form element by name, we tell it to "grab the requested information from the form element that contains this handler." Both methods work equally well, and it's always good to be exposed to multiple ways of accomplishing a task.

With the insertion of the new event handler, our script is finished and ready to work. With the onChange handler, the page now changes as soon as the user makes a choice from the menu, without having to press a button.

Reviewing the Script

Let's look at what we did to create this script. We first created a function that accepts a value from a form element and uses it to send the user to another HTML page. We also went over two different event handlers that can be used to drive the scripts.

First, let's look at the function.

// Pull-Down Menu Navigation Function
function PageChanger(page) {
     document.location= page;
     document.NavForm.PullDown.options[0].selected=true;
}
  1. We created the function PageChanger().

  2. We set the location property of the DOCUMENT object to the value contained within the variable page. This value is the URL that is being passed into the function from the pull-down menu.

  3. We reset the option that shows in the pull-down menu to the first option.

Now let's look at the HTML needed for use with the onClick handler.

<select name="PullDown">
     <option value="">Get There Quick
     <option value="whatsnew.html">What's New
     <option value="featured_product.html">Featured Product
     <option value="press.html">Press Releases
     <option value="store.html">Company Store
</select>

<input type="button" name="Go" value="Go" onClick="PageChanger(document.NavForm.PullDown
graphics/ccc.gif.options [NavForm.PullDown.selectedIndex].value)">
   
  1. Our function is called when the user clicks on the Go button. We inserted the onClick event handler into the button form element.

  2. Within the event handler, we call our PageChanger() function and pass into it the value held by the menu option that has been chosen from the pull-down menu.

  3. Once we decided to take the Go button off the page because of design issues, we used the onChange event handler instead.

    <select name="PullDown" onChange="PageChanger(this.options[this.selectedIndex] .value)">
         <option value=" ">Get There Quick
         <option value="whatsnew.html">What's New
         <option value="featured_product.html">Featured Product
         <option value="press.html">Press Releases
         <option value="store.html">Company Store
    </select>
    
  4. We inserted the onChange event handler into our pull-down menu. Again within this event handler, we call our PageChanger() function and pass into it the URL of the selected menu option.

Let's take a look at the new concepts that we have covered during this project.

  • We learned how to access and change the location property of the DOCUMENT object.

  • We learned how to access the values of pull-down menu options and how to change which option is currently selected within a pull-down menu.

  • We were introduced to two new event handlers, onClick and onChange.

As Web sites become more and more important to the success of companies, the amount of content contained in them grows by leaps and bounds. Finding quick and efficient methods to navigate the information is now more important than ever. The two methods just discussed are definitely useful.

  • + Share This
  • 🔖 Save To Your Account