DHTML Lab: Hierarchical Menus Version 3 | 5 | WebReference

DHTML Lab: Hierarchical Menus Version 3 | 5


Hierarchical Menus: Version 3
menu hiding


Click the link above to reveal menu. Click anywhere on the page to hide menu.

Parameters used for the menus on this page:

menuVersion = 3;
menuWidth = 140;
childOffset = 5;
perCentOver = 30;
secondsVisible = 1;
fntCol = "black";
fntSiz = "10";
fntBold = true;
fntItal = false;
fntFam = "serif";
backCol = "#2D9B83";
overCol = "#4BB9A1";
overFnt = "black";
borWid = 2;
borCol = "darkgreen";
borSty = "solid";
itemPad = 3;
imgSrc = "tri.gif";
imgSiz = 10;
separator = 1;
separatorCol =
isFrames = false;
keepHilite = true; 
NSfontOver = false;
clickStart = true;
clickKill = true;
showVisited = "";

Background Reading:

hide functions:
  column 14 (1)
  column 14 (2)
  column 20

In script listings, cross-browser code is blue, Navigator-specific code is red, and Explorer code is green.

The [cc] symbol denotes code continuation. The code is part of the preceding line. It is placed on a new line for column formatting considerations only.


The hideAll() function is called to hide all menu trees. In version 2, it looped through all created top-level menus (topCount), and hid any visible children, then the top-level menu. Since version 3 allows any child menu to be the first-displayed menu, our for loop identifies the menu stored in a top-level menu's startChild property. It then hides children lower in the hierarchy than startChild, and finally startChild itself:

function hideAll() {
    for(i=1;i<topCount;i++) {
        temp = eval("elMenu" + i + ".startChild");
        temp.isOn = false;
        if (temp.hasChildVisible) temp.hideChildren();

Menu Tree Hiding

The three functions used to hide parts or all of a menu hierarchy, hideTree(), hideParents() and hideChildren(), have not changed since version 2:

function hideTree() { 
    allTimer = null;
    if (isOverMenu) return;
    if (this.hasChildVisible) {
function hideParents() {
    tempMenu = this;
    while (tempMenu.hasParent) {
        tempMenu.parentMenu.isOn = false;        
        tempMenu = tempMenu.parentMenu;
function hideChildren(item) {
    tempMenu = this.visibleChild;
    while (tempMenu.hasChildVisible) {
        tempMenu.hasChildVisible = false;
        tempMenu = tempMenu.visibleChild;
    if (!this.isOn || !item.hasMore || this.visibleChild != this.child) {
        this.hasChildVisible = false;


The hideTop() function calls hideSelf() to hide the first-displayed menu in a menu tree (startChild). In previous versions, it used a timer variable to delay the call to hideSelf(). In version 3, where a click event can hide a menu tree, hideSelf() is either called immediately (when clickStart is true) or with a delay, as before.
function hideTop() {
    whichEl = this;
    (clickKill) ? whichEl.hideSelf() :
[cc]   (this.hideTimer = setTimeout("whichEl.hideSelf()",mSecsVis));


Finally, hideSelf(), which hides the first-displayed menu in a tree, remains the same:

function hideSelf() {
    this.hideTimer = null;
    if (!this.isOn && !isOverMenu) { 

Hiding on a click

Recall that if we have set clickKill to true, then everytime the user clicks in the page, the clicked() function is called:

function clicked() {
    if (!isOverMenu && currentMenu!=null && !currentMenu.isOn) {
        whichEl = currentMenu;

clicked() first checks to see if the user's mouse is over a menu (isOverMenu). If it isn't, it proceeds to verify that a visible menu exists and that its isOn property is not true. If all the conditions are met, it hides the currentMenu tree.

Only a couple more, equally painless, pages to come. The first addresses those nasty "denial" error messages.

Produced by Peter Belesis and

All Rights Reserved. Legal Notices.
Created: Sept. 03, 1998
Revised: Sept. 03, 1998

URL: http://www.webreference.com/dhtml/column21/hier3Hide.html