advertisement

Create a vertical pulldown script that displays navigational headers and when you click on a header new links appear underneath it, this effect works both in Firefox and Internet Explorer.
We will be doing all of our code in just 1 file to make this easier, later on you can split it all up into different files for your applications but for the ease of this tutorial everything will be in the same file.

This is a great effect for websites that have limited room and want to clean up there navigation a bit. It also provides visitors to your website the choice to display certain links or not, if there are links they do not want to see they can just hide them.

So first we need to start off by creating our HTML document and defining our javascript function in the of our document.

<html>
<head>
<title>Creating javascript pulldown menus</title>
<script language="Javascript" type="text/javascript">
<!-- Hide from old browsers

function displayMenu(currentMenu) {
var thisMenu = document.getElementById(currentMenu).style

// The above creates a new function that will expand and contract certain menus that are specified.

if (thisMenu.display == "block") {
thisMenu.display = "none"
} else {
thisMenu.display = "block"
}
return false
}
// -->
</script>

So the above piece of code may seem a bit long, confusing and daunting and first, but fear not it is actually quite easy to understand. All we are doing is starting a <script> section in the <head> of our HTML document, then we define a new function. This is the function we will be using to actually contract and expand the menus. It just says, if the menu is already expanded, then contract it and then we have a } else { statement that says if it is already contracted, expand it.

Now we need to do a bit of CSS work for our menus to work properly.

<style type="text/css">
.menu {
display: none;
margin-left: 15px;
color: #000;
}
.menu a {
color: #000;
}
.menu a:hover {
color: #B22222;
margin-left: 20px;
}
</style>
</head>

The above piece of code is the CSS in our menu, Im just putting this in so that it may be a BIT more aesthetically pleasing to our eyes, obviously if you use this script in your own websites/applications then you style the menus to how you like.

Now we start the actual body of our HTML document and start to create our menus.

<body>
<h3>Click on a menu header to expand or contract it</h3>

<b><a href="#" onClick="return displayMenu('tutMenu')">Tutorial Categorys</a></b><br />

I thought I would cut it off there and explain that little snippet of code, what we are doing here is creating a text link and using an event handler on it. We are using the onClick event handler. What this does is ON CLICK of the link we execute an action. In the case of this piece of code we are calling our displayMenu function that we created earlier. You may also notice the piece of text in there that says ‘tutMenu’. This is defining the name of this specific menu, because if we didnt name our menus then the javascript function wouldnt know which menu to expand or contract.

Now we are going to create the navigational options that you get when you click on the header.

<div class="menu" id="tutMenu">
<a href="#">PHP Tutorials</a><br />
<a href="#">Javascript Tutorials</a><br />
<a href="#">CSS Tutorials</a><br />
<a href="#">Kick Arse Tutorials</a><br />
</div>

This is our navigational menu, we are using the menu class we created in our CSS style before. And I just put # in the menu links just for filler, when you use this script in an actual website application you would change those # to the actual links you want to visit when you click on the tutorials.

Now I am just going to create another 2 menu headers and 2 more sets of menu navigation links for more filler and to give you an idea of what it looks like and how it works with more then 1.

<b><a href="#" onClick="return displayMenu('linksMenu')">Links</a></b>
<div class="menu" id="linksMenu">
<a href="#">Home</a><br />
<a href="#">About Us</a><br />
<a href="#">Contact Us</a><br />
<a href="#">Send Us Hate E-mail</a><br />
</div>

<b><a href="#" onClick="return displayMenu('randomMenu')">Random</a></b>
<div class="menu" id="randomMenu">
<a href="#">Funny Animals</a><br />
<a href="#">Funny People</a><br />
<a href="#">Funny Websites</a><br />
<a href="#">Funny Webmasters</a><br />
</div>
</body>
</html>

So all I’ve done above is create 2 new menus as filler menus.

Now for all you people that would like to see the completed code in its entirity here you go -

<html>
<head>
<title>Creating javascript pulldown menus</title>
<script language="Javascript" type="text/javascript">
<!-- Hide from old browsers

function displayMenu(currentMenu) {
var thisMenu = document.getElementById(currentMenu).style

// The above creates a new function that will expand and contract certain menus that are specified.

if (thisMenu.display == "block") {
thisMenu.display = "none"
} else {
thisMenu.display = "block"
}
return false
}
// -->
</script>
<style type="text/css">
.menu {
display: none;
margin-left: 15px;
color: #000;
}
.menu a {
color: #000;
}
.menu a:hover {
color: #B22222;
margin-left: 20px;
}
</style>
</head>
<body>
<h3>Click on a menu header to expand or contract it</h3>

<b><a href="#" onClick="return displayMenu('tutMenu')">Tutorial Categorys</a></b><br />
<div class="menu" id="tutMenu">
<a href="#">PHP Tutorials</a><br />
<a href="#">Javascript Tutorials</a><br />
<a href="#">CSS Tutorials</a><br />
<a href="#">Kick Arse Tutorials</a><br />
</div>
<b><a href="#" onClick="return displayMenu('linksMenu')">Links</a></b><br />
<div class="menu" id="linksMenu">
<a href="#">Home</a><br />
<a href="#">About Us</a><br />
<a href="#">Contact Us</a><br />
<a href="#">Send Us Hate E-mail</a><br />
</div>

<b><a href="#" onClick="return displayMenu('randomMenu')">Random</a></b><br />
<div class="menu" id="randomMenu">
<a href="#">Funny Animals</a><br />
<a href="#">Funny People</a><br />
<a href="#">Funny Websites</a><br />
<a href="#">Funny Webmasters</a><br />
</div>
</body>
</html>

Notes
When you are using the <div class=” ” id=” “> you MUST define the id=” ” as the same name that you defined in the head in the displayMenu(”) function. So for example, in one of the menus I have defined the displayMenu(”) parameter as randomMenu, so for my id=” ” I must define it as id=”randomMenu”.

You must also remember that anyone with javascript disabled in there web browsers will not be able to use this navigation, so it is always a good idea to provide alternatives to your main navigation and provide links in your footer, this is also a good idea for Search Engines, because a lot of search engine spiders do not have javascript and will not be able to crawl any of the links located in your navigation. So a fix for that is to just place your links in the footer of your website to provide maximum usability.

Well, thank you for taking the time to read my tutorial. I hope you have come away from this tutorial with some new knowledge.

Thanks
Sean

advertisement