This chapter is from the book

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.