Any global navigation system needs a way
to call different Web pages and the drop-down menus need a way to call
a selected item. Up to this point, the <a>
tag has done a good job of taking care of links, but you probably noticed the drop-down menus have no links. The <select>
tag needs to work with the form
element and a JavaScript
function. On the HTML5 side, the following snippet shows the
essentials:
<
form name=
”menuNow”
>
<
label for=
”animals”
>
Animals</
label>
<
select id=
”animals”
name=
”global1”
onChange=
”optionMenu()”
>
<
option value=
”animals/horses.html”
>
Horses</
option>
<
option value=
”animals/dogs.html”
>
Dogs</
option>
The names of the form
and select
elements are important because JavaScript uses the names as a path to the selected option
. (If you’re familiar with arrays, the options are all treated as array elements.)
The JavaScript is placed in a separate file because
if you’re going to be using it with a global navigation system, you
don’t want to have to rewrite it with every page. The following
JavaScript should be saved in a text file named globMenu.js
.
function optionMenu()
{
var choice =
document.
menuNow.
global1.
selectedIndex;
var urlNow =
document.
menuNow.
global1.
options[
choice].
value;
window.
location.
href =
urlNow;
}
What that reflects is the HTML5 Document Object Model (DOM). The document
is the Web page, menuNow
is the name of the form
element, global1
is the name of the select
element, and selectedIndex
is the selected option. Because the selectedIndex
is a number between 0 and the number of options in the <select>
tag container, it can be used to choose the array element (option),
which is selected. Whatever value is stored in the option is passed to
the variable named urlNow
. For example, the following line has a relative URL of animals/dogs.html
:
<
option value=
”animals/dogs.html”
>
Dogs</
option>
The final line in the JavaScript, window.location.href = urlNow
, has the same function as the following HTML5 line:
<
a href=
”animals/dogs.html”
>
In this context, a different JavaScript function would have to be written for each <select>
tag because the function uses a specific reference to that tag (global1
).
More sophisticated JavaScript could be developed to use variables for
the different element names, but the function employed here is
relatively short and easier to implement.
To test this out yourself, create simple Web pages with the following names:
• horses.html
• dogs.html
• cats.html
• rabbits.html
• raccoons.html
The Web pages can just have names on them — nothing fancy.
<!
DOCTYPE HTML>
<
html>
<
head>
<
script type=
”text/javascript”
src=
”globMenu.js”
/>
<
meta http-
equiv=
”Content-Type”
content=
”text/html; charset=UTF-8”
>
<
title>
Drop-
Down Menu</
title>
</
head>
<
body>
<
article>
<
header>
<
nav>
<
form name=
”menuNow”
>
<
label for=
”animals”
>
Animals</
label>
<
select id=
”animals”
name=
”global1”
onChange=
”optionMenu()”
>
<
option value=
”animals/horses.html”
>
Horses</
option>
<
option value=
”animals/dogs.html”
>
Dogs</
option>
<
option value=
”animals/cats.html”
>
Cats</
option>
<
option value=
”animals/rabbits.html”
>
Rabbits</
option>
<
option value=
”animals/raccoons.html”
>
Raccoons</
option>
</
select>
<
label for=
”vegetables”
>
Vegetables</
label>
<
select id=
”vegetables”
name=
”global2”
>
<
option value=
”carrots”
>
Carrots</
option>
<
option value=
”squash”
>
Squash</
option>
<
option value=
”peas”
>
Peas</
option>
<
option value=
”rice”
>
Rice</
option>
<
option value=
”potatoes”
>
Potatoes</
option>
</
select>
<
label for=
”minerals”
>
Minerals</
label>
<
select id=
”minerals”
name=
”global3”
>
<
option value=
”tin”
>
Tin</
option>
<
option value=
”gold”
>
Gold</
option>
<
option value=
”copper”
>
Copper</
option>
<
option value=
”iron”
>
Iron</
option>
<
option value=
”mercury”
>
Mercury</
option>
</
select>
</
form>
</
nav>
</
header>
</
article>
</
body>
</
html>
Test the page using with Google Chrome or Opera —
at the time of this writing, those were the only two browsers that had
implemented this aspect of HTML5.