Active Menu

Introduction

This file contains a collection of functions which add interactivity and formatting to a definition list. The interactivity consists of a list of headlines positioned next to a box which contains the story associated with the headline. When a headline is moused over it will be highlighted and it's story shown in the box. The format of the underlying code are two lists that are side-by-side.

The interactivty uses DOM-compliant coding and as a result older browsers (such as Netscape Navigator 4.x) will not perform any of the operations. Also, the functionality of the script was design so that if javascript were disabled or the page accessed by an older browser the user would still be able to view the content of the page.

Requirements

A W3C DOM-compliant browser with JavaScript and CSS support enabled. Tested in both standards mode and quirks mode.

Tested and functional:

Tested and non-functional:

Usage

Attach the JS file (formatted or size-optimized) to a document using <script src...></script> or some other mechanism.

Include in the html document a stylesheet that contains the basic style information. Additional styling can be defined as desired, but the basic style information must be included for the script to function as expected.

The menu uses a <div> with an id attribute of "activeMenuBox" for the container object. Inside the <div> should be another <div> with an id attribute of "activeMenuList". activeMenuList is filled with the headlines dynamically and right-floated by default, there is no need to manually enter any information in this container. Following activeMenuList should be a definition list . The <dt>s of this list should contain the linked headlines. The <dd>s of this list, of which there can be multiple for each headline, should contain the associated story. By default images inside a <dd> are floated to the left.

Modify user parameters as desired. The following parameters can be set on the web page referencing this script after the script reference.

activeMenu_bolResize2Fit
[boolean] Indicates whether or not to resize the story box to fit the height of the longest story. By default the story box is set to a height defined in the stylesheet. When set to false, the story box will have a scroll bar when it is displaying a story that is too long to fit.
Default: false
activeMenu_intActiveItem
[integer] The zero-based index of the news item to show when the menu has completed initialization.
Default: 0
activeMenu_strEvent
[string] A string identifying the event on the list of headlines that changes the content of the story box. These can be any of the standard events (such as click, mouseover, mouseup, or focus).
Default: "mouseover"
activeMenu_objInsertBefore
[string] The ID of an object contained within the activeMenuList <div> that the headline list should be inserted before.
Default: "" (empty string)

Sample

  1. <html>
  2. <head>
  3. <title>Active Menu Sample</title>
  4. <link rel="stylesheet" type="text/css" href="activeMenu.css">
  5. <script type="text/javascript" src="activeMenu.js"></script>
  6. </head>
  7. <body>
  8. <div id="activeMenuBox">
  9. <div id="activeMenuList"></div>
  10. <dl>
  11. <dt><a href="http://www.google.com">Headline #1</a>
  12. <dd><img src="g.gif" alt="Placeholder Image" width="85" height="85"></dd>
  13. <dd><p>If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect. <a href="http://www.google.com">Check it out</a>!</p></dd>
  14. <dt><a href="http://www.google.com">Headline #2</a></dt>
  15. <dd><p>If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect. <a href="http://www.google.com">Check it out</a>!</p> <p>If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect. <a href="http://www.google.com">Check it out</a>!</p></dd>
  16. <dt><a href="http://www.google.com">Headline #3</a></dt>
    <dd><p>If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect. <a href="http://www.google.com">Check it out</a>!</p></dd>
  17. <dt><a href="http://www.google.com">Headline #4</a></dt>
  18. <dd><p>If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect. <a href="http://www.google.com">Check it out</a>!</p></dd>
  19. <dd><p>If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect. <a href="http://www.google.com">Check it out</a>!</p></dd>
  20. </dl>
  21. </div>
  22. </body>
  23. </html>

Comments on the sample:

See the demo page for a demonstration of the features of this script.

Functions

The following functions are included in the show/hide code library. Additional line-item documentation is provided in the code itself. Source information for public, externally developed functions is provided; documentation for these functions is available from the source.

Function activeMenu_addEvent
Source Scott Andrew LePera
http://www.scottandrew.com/weblog/articles/cbs-events
Cross-browser code to add an event handler to a DOM object
Parameters
obj
[object] the DOM object to which an event is being attached
evType
[string] the event handler being added ('click', 'mouseover', etc.)
fn
[function] the function called by the event handler
useCapture
[boolean] determines if the event fires during capture or bubble
Notes

2005-09-20 (bsweeney@aaas.org)

  • removed alert for unsupported browsers

 

Function sh_lib_bwcheck
Source DHTML Central
http://www.dhtmlcentral.com/projects/lib/reference.asp?m=58
Creates an object that stores information about the user agent
Parameters None
Notes

2006-05-05 (bsweeney@aaas.org)

  • added a check for IE7

2005-09-20 (bsweeney@aaas.org)

  • added a check for Safari

 

Function activeMenu_init
Details

This function initializes the activeMenu process. The activeMenu container element is given a class of activeMenu (many of the relevant styles are associated with this class to avoid having them applied in browsers that are unable to process the script). The definition list inside the activeMenu container is parsed for the <dt> (containing the story headline) and <dd> (containing the story detail) elements that comprise the story. Each story is given a unique class name, determined upon encountering a <dt>, which is assigned to the <dt> and <dd>s. A reference to the link contained in each <dt> is pushed onto the araStoryLinks array (used for creating the headline list) and references for the <dt>s and <dd>s are pushed onto the araStoryDetail array (used to access the story elements).

Next the script creates an unordered list with each <li> in this list containing a link from the araStoryLinks array. These links are given an id associating it with the story and an event (determined based on the value of activeMenu_strEvent) is set that calls the activeMenu_change() function. Each link is pushed onto the araStoryList array. When the list is complete it is appended to the DOM inside the activeMenuList container element.

If the activeMenu_bolResize2Fit variable is set to true then the next step is to calculate the "height" of each story. By default the elements of the DL associated with activeMenu are set to display: none, which makes a height calculation impossible. First an individual story is made visible using the activeMenu_change() function. Then the height of that story is calculated. If no height is set on the DL then we can calculate the maximum height accurately based on the height of the DL with each individual story showing. Otherwise we need to calculate the height of the DT and DDs associated with a story. The second method is not as accurate because the margins of an element are not included in the calculation. A fudge factor is assessed based on the number of DDs, but this may not always produce the correct calculation. Additionally, the calculation of the height used by a DD with an image is complicated by the fact that images are floated by default. Since the image is floated two problems are encountered: the height of the DD is effective zero (in gecko-based browsers; the height of the image is not an accurate measurement of the space used by the DD. To get around this problem the height of a DD with an image is calculated by multiplying the image height by a percentage based on the area used by the image inside a box surrounding the image and covering the full width of the DL.

Finally the activeMenu_change() function is called with the index of the item to be shown on load (as indicated by the activeMenu_intActiveItem variable ).

Parameters None (used as event handler)
Notes

2006-01-27 (bsweeney@aaas.org)

  • The height calculation only works if there is not height set on the activeMenu DL element or its child nodes or if the set height is less than the maximum height of each story (DT+DD). Otherwise the height set by the stylesheet is used.

 

Function activeMenu_change
Details This function controls which story is displayed and which headline in the headline list is highlighted. First the araStoryList array is parsed. The element with an id matching the storyID is given a class of storySelected while the others have this class removed. Next the araStoryDetail array is parsed. The elements with a class matching the storyID are given a class of storySelected while the others have this class removed.
Parameters
storyID
The identifier (class name) of the story to be shown.
Notes

2006-01-27 (bsweeney@aaas.org)

  • Modified to require only the story index

 

To Do

Notes

2006-05-17 (bsweeney@aaas.org)

2006-02-27 (bsweeney@aaas.org)

2006-01-27 (bsweeney@aaas.org)

2005-11-22 (bsweeney@aaas.org)

2005-09-29 (bsweeney@aaas.org)

2005-09-20 (bsweeney@aaas.org)