Trying to maintain a common menu across a dozen or more html files has bugged me for a while now. I lamented the lack of a C style include statement in html. So, I finally set my programmer mind the task of solving this problem, and here it is.
First, I created the following javascript functions:
1. A function to create a menu entry.
function PutMenuEntry(file, title)
{
if (document.title != title)
{
document.write("<div class=" "menu_entry" ">");
document.write("<a href=" file ">" title "</a>");
document.write("</div>");
document.write("<br>");
}
}
2. A function to create the full menu:
function CreateMenu()
{
document.write("<div id='menu'>");
PutMenuEntry("index.html", "Home");
PutMenuEntry("about_us.html", "About Us");
PutMenuEntry("members.html", "Members");
PutMenuEntry("resources.html", "Resources");
PutMenuEntry("product_list.html", "Products");
PutMenuEntry("contact_us.html", "Contact Us");
PutMenuEntry("related_links.html", "Links");
PutMenuEntry("events.html", "Events");
document.write("</div>");
}
Then, I included this in each of my html files:
<script>
CreateMenu();
</script>
Works like a champ! Now the actual menu in maintained in only one place (the script file) and the appearance is controlled in one place (the CSS style file). If I add another page, I only need to add a "PutMenuEntry" line to the "CreateMenu" function in the script file and the new button appears on the menu on all the pages.
Only thing difficult is that the title of an html file must be the same as the label on the menu button if you want it's button to not show up when that page is displayed. If you don't mind having a menu button that just reloads the current page, then it doesn't matter. Or... you could get ambitious and add another parameter to the "PutMenuEntry" function to use for disabling the button for that page.
Oh, you might be interested in the CSS that makes this look like a real menu. Here it is:
The menu itself:
#menu /* menu only appears once on any given page */
{
position: absolute;
top: 140px;
left: 10px;
height: auto;
width: 120px;
}
The menu entries (the buttons):
.menu_entry
{
border-style: solid;
border-width: 2px;
border-color: black;
border-radius: 5px;
padding: 5px;
font-size: 18.0px;
text-align: center;
color: green;
background-color: lightgreen;
}
.menu_entry a:hover
{
font-weight: bold;
color: blue;
}
So, feel free to use any of this you find helpful. Enjoy and think of me....