Navigation Bar
  Mouse over the headers at the top of the page and you'll get a drop down menu. The individual menu items are highlighted when under the cursor and can be clicked to link to a new page. The headers themselves can also be set as links.

This all new version of the Navigation Bar script has some enhanced features, some bug fixes and is easier to construct than the original version. It's also larger but that's the price of functionality.

New Features

Headers and menus can now be set with individual widths or you can let them default to fit the header text. Bars can be resized dynamically, and the headers can be aligned to the left, center or right. Try it out with the links below. (Note that a second bar is defined but is initially hidden.)

NavBar Method Examples
Gray NavBar Move/Resize 1 Move/Resize 2 Move/Resize 3
Align Left Align Center Align Right
Align 50 px. Align 100 px. Align 150 px.
Teal NavBar Show Bar Hide Bar Invert Bar

You can now create as many navigation bars as you want. Each can have it's own headers and drop down menus with a unique set of colors and fonts. Individual bars can be manipulated independently using the provided methods.

Note that the stacking order of the bars is changed in the above examples so that whichever bar is closest to the top of the page will appear on top of the other when they overlap. Also note that the menus can be made to open above the bar.

Using the Script

First you'll need to include either both the dhtmllib.js and navbar.js files in your page or the single, condensed navcond.js file. See the notes on the Source section at the end of this article.

Then you add some code to define each bar and add it to the page. There are three distinct steps to creating a navigation bar.

  1. Define - First you must to define a new NavBar object. In this stage you set its appearance and define the drop down menus and items for it. This can be done either as the page is loading or afterwards.

  2. Build - The NavBar object has a create() method that dynamically builds the DHTML elements of the bar and adds them to the page. This can only be called after the page has finished loading.

  3. Manipulate - After the bar has been created and added to the page, you can position it, hide or show it or change some of its attributes like width, alignment and stacking order.

The details of each step are outlined below.

I. Defining a Navigation Bar

A navigation bar is made up of three distinct objects: one for the bar itself, one for drop down menus and one for individual items. You use these objects and their associated methods to construct a bar. Here is some sample code.

  var myNavBar, menu;

  myNavBar = new NavBar(500);
  myNavBar.setSizes(1, 2, 1);
  myNavBar.setColors("#000000",
    "#ffffff", "#669999", "#000000", "#66cccc",
    "#ffffff", "#339999", "#000000", "#99ffff");
  myNavBar.setFonts("Arial, Helvetica", "plain", "bold", "10pt",
    "Arial, Helvetica", "plain", "bold", "10pt");

  menu = new NavBarMenu(80, 100);
  menu.addItem(new NavBarMenuItem("Header 1", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 1-1", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 1-2", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 1-3", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 1-4", "blank.html"));
  myNavBar.addMenu(menu);

  menu = new NavBarMenu(80, 100);
  menu.addItem(new NavBarMenuItem("Header 2", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 2-1", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 2-2", "blank.html"));
  menu.addItem(new NavBarMenuItem("Item 2-3", "blank.html"));
  myNavBar.addMenu(menu);

  ...

You can place the code to define a navigation bar anywhere on your page although generally you would place it between the <HEAD> and </HEAD> tags of your HTML code so that it can run as the page loads.

The constructors and methods for each object are described below.

The NavBar Object

This object defines the navigation bar itself. Once you define a NavBar you can set its colors, fonts, etc. and add menus to it using the methods described below.

  • NavBar(width) - Creates a new NavBar object with the given width. If a width of zero is specified, the bar will be sized automatically to fit the headers assigned to it.

  • setSizes(border, padding, separator) - Defines the width of the border around headers and menus, the amount of spacing surrounding the item text and the width of the border between each item in a drop down.

  • setColors(bdColor, hdrFgColor, hdrBgColor, hdrHiFgColor, hdrHiBgColor, itmFgColor, itmBgColor, itmHiFgColor, itmHiBgColor) - Sets the various colors used for the bar. The first value sets the border color. The next two set the text and background colors for a header and the two after that set the colors used when a header is highlighted. The last four values do the same for the menu items.

  • setFonts(hdrFamily, hdrStyle, hdrWeight, hdrSize, itmFamily, itmStyle, itmWeight, itmSize) - Defines the font used for the headers and the menu items. Standard CSS values may be used.

  • addMenu(menu) - Adds a menu to the navigation bar. The given menu must be a NavBarMenu object. You can add as many drop down menus to the bar as you want. The first item will be used as the header displayed on the bar.

There are additional methods defined for the NavBar object which are discussed later but the ones shown here are used strictly for constructing a navigation bar and defining its appearance. They must all be used before the bar is actually built and added to the page otherwise they will have no effect.

The NavBarMenu Object

This object defines a single header and drop down menu.

  • NavBarMenu(hdrWidth, menuWidth) - Creates a new NavBarMenu object. The first value sets the width used for the header. If this is set to zero, the header will be sized to fit the text for it. The second value sets the width of the drop down menu. If this value is set to zero, it will be set to match the width of the header.

  • addItem(item) - Adds an item to the menu. The given item must be a NavBarMenuItem object. The first item added to a menu will be used as the header displayed on the bar while the rest will appear in the drop down menu. You can add as many items as you want to the menu.

The NavBarMenuItem Object

  • NavBarMenuItem(text, link) - Creates a new NavBarMenuItem object with the given text and link.

Setting Item Links

The text for a menu item can include simple HTML tags like <CENTER></CENTER> or <IMG> tags along with the plain text. The link can be one of three things: a URL, a null string ("") or a string of JavaScript code (any string beginning with "javascript:").

For a URL, the link is loaded into the current window or frame when the item is clicked on. When a null string is specified, it creates a 'dead' link. Clicking on the item does nothing, but it will still be highlighted and if it is a header, the drop down menu will still appear when it is moused over.

Menu items are not like normal HTML links (<A HREF="...">). So you cannot specify a target frame or window or event handlers like onmouseover. You can, however, use JavaScript code to achieve some of the same effects.

Any link string that begins with "javascript:" is executed using the built-in eval() function when the item is clicked. This means that you can load pages in frames like this.

  new NavBarMenuItem("Table of Contents",
                     "javascript:top.frames['main'].location = 'toc.html'");

Which loads the URL 'toc.html' into a frame called 'main'.

You can open a link in a new browser window using the window.open() method. The code below produces the same result as using TARGET="_blank" in a link.

  new NavBarMenuItem("Table of Contents",
                     "javascript:window.open('toc.html')");

See the notes on Using Navigation Bars in Frames if you intend to use the script in a frameset.

II. Building a Navigation Bar

Once you've defined a navigation bar and its menus you need to build the necessary DHTML elements for it and add it to the page. This is done by simply calling the NavBar create() method.

You can only call create() after the page has finished loading. Once created, you cannot change the colors or fonts of the bar or add new menus or items to it.

The easiest way to do this it so set up a function to call via the onload event in the <BODY> tag. Here is a typical page setup.

  <html>
  <head>
  <title></title>
  <script language="JavaScript" src="dhtmllib.js"></script>
  <script language="JavaScript" src="navbar.js"></script>
  <script language="JavaScript">

  // Define the navigation bars.

  var myNavBar1 = new NavBar(400);
  var myNavBar2 = new NavBar(0);

  /* ... see code above for configuring the navigation bars ... */

  function init() {

    // Create the navigation bars.

    myNavBar1.create();
    myNavBar2.create();
  }

  </script>
  </head>
  <body onload="init()">

  <!-- your page contents -->

  </body>
  </html>

Once the bar has been added to the page, you can reposition it, resize it, hide and show it, etc. as desired.

Page 1 - Page 2


Home