Sample Menus

Dynamic HTML Menus -- Using The JavaScript Menu Component

The purpose of this page is to provide sample menus illustrating the various styles and different ways you can create menus using the Menu component. If your site uses the Menu component, you can submit your URL for possible posting here in a future update of this page.

Note that you must enable JavaScript to run these examples.

  • Local sample menus:
  • myMenu
  • Colors Menu
  • Yellow/Blue Menu
  • Submenus
  • Technologies Menu
  • JavaScript Menu
  • Brick-Red Menu
  • Mozilla Menu
  • Search Menu
  • Big-Borders Menu
  • Black Menu
  • White Menu
  • No-Borders Menu
  • Creating Menus

    For details on how to create menus, see the article The JavaScript Menu Component in View Source. That article explains how to create, add, and customize menus for your own web site. For a quick summary of how to make a menu, read through the following tutorial.

    Adding a menu to your web site is easy. To begin, add the following line of code into your web page:

    <SCRIPT LANGUAGE="JavaScript1.2" SRC="http://developer.netscape.com/openstudio/lib/menu.js"></SCRIPT>
    
    Next, create a menu object by using the JavaScript new operator and the Menu() constructor, as follows:
    var myMenu = new Menu();
    

    This statement creates a menu object called myMenu. You can add "menu items" to your newly created menu object with the addMenuItem() method, which is built into the Menu component. For example:

    myMenu.addMenuItem("my item");
    

    To continue adding menu items, keep calling addMenuItem(), as shown in the following sample script.

    <SCRIPT LANGUAGE="JavaScript1.2">
    <!--
        var myMenu = new Menu();
    
        myMenu.addMenuItem("my menu item A");
        myMenu.addMenuItem("my menu item B");
        myMenu.addMenuItem("my menu item C");
        myMenu.addMenuItem("my menu item D");
    
        myMenu.writeMenus();
    //-->
    </SCRIPT>
    display myMenu

    Let's summarize the three basic steps it takes to quickly add a dynamic menu to your web page:

    1. Copy and paste the <SCRIPT> tag for adding the Menu library.
    2. Copy and paste the menu script from the example.
    3. Copy and paste the display myMenu link.
    Again, for more information on creating or customizing multiple menus, please read the View Source article, or view the source of this page and check out the menu objects.

    Menus in Frames

    HTML and CSS-P-based content cannot overlap across frameset borders -- just as images can't. Therefore, just as with images, you need to choose which frame you want the menus to be displayed in. In the frame you choose, do everything as usual for creating your menus and loading them into the frame. Then, so that another frame can make calls to display the menus in your chosen menus frame, you need to create a function that calls the showMenu() function in the menus frame. This can be done by using the window object hierarchy of the browser to jump from one frame to another, because a window containing a frameset has a root parent (or top) object that contains multiple children (window and document) objects, one per page. The following examples show how to do this: These examples use a new event handler function called showMenuInFrame(). Depending on where you want the menus to be displayed on your site, this function may vary. (This is why I haven't added it into the Menu component library; however, it can be added at your request.) View the page source of the first (toc) frame to see how it's done. The trick here is that you need to do a little more code sharing and create a event handler, like showMenuInFrame(), in the frame that makes the call to display the menu in the menus frame.

    The showMenuInFrame() function, which is in the first frame of the examples, passes the menu label and the x and y coordinates as arguments to the showMenu() function, which is actually located in the second frame along with the Menu component and the CSS-P-based menu content. There are other ways to do this, but this seems to be the simplest. Alternatively, I could have called the showMenu() function directly -- and you still can -- but the x and y coordinates might not be what you expected (unless you explicitly hard-code them), because the mouse is actually over the first (toc) frame at that time, not the second frame where the menus are. (Remember, each frame has its own separate window and document objects, containing a separate set of x and y properties.)

    Note that the second frame creates and loads the menus the same way that this page does, but instead of having the menu objects code in the page itself, it includes a JavaScript library called mymenus.js that contains all the code for the menu objects (Open Studio also uses a library called openstudio.js for sharing menu objects). This way I can include the mymenus.js library (along with the menu.js library) into multiple documents in order to share and cache the same menu objects code.

    If you need help, get as far as you can and then e-mail your staged URL. Based on your feedback, we can come up with something more generic and compatible for everyone.


    Revision History

    The 0.3 version of the Menu component is actually more flexible and robust (see also other objects for Visual DHTML). However, this earlier version is more for web applications than web sites, because it takes longer to build, and it doesn't run on Internet Explorer 4, since that IE version wasn't around at the time. For cross-browser web pages, I recommend using the latest version of the Menu component.

    As you can see, the Menu component still hasn't reached a "1.0" version yet (although it should soon, with your help). That's because originally I created the Menu component as just sample code for Netcaster Channel developers to copy and modify, with the idea of adding features and improving the portability of the component as developers requested it, until it reached a fuller-featured state. However, back then I never got around to actually publishing the Menu component. Now that I have, I still plan to update and improve it based on feedback from developers, so please send in your suggestions, requests, or comments regarding this object.


    Further Resources


    DevEdge Online FAQ
    Developer Response Center
    Join DevEdge Program
    Copyright © 1998 Netscape Communications Corporation.
    This site powered by: Netscape Enterprise Server and Netscape Compass Server.