Content in the Javascript library assumes a sound understanding of ECMAScript, CSS, XHTML and DOM.

Menubar script

Code listing for mb.js

Hover over each line to read tips.

  1. /*
  2. * date: 2003-04-28
  3. * info: http://inspire.server101.com/js/mb/
  4. */
  5. var mbW=250;
  6. var mbO=[],mbP=[],mbA,mbT,mbTf;
  7. function mbSet(m,c) {
  8. if (document.getElementById&&document.createElement) {
  9. var m=document.getElementById(m);
  10. m.className=c;
  11. var e,f,i,j;
  12. var e=m.firstChild;
  13. while (e) {
  14. if (e.tagName=='LI') {
  15. var div=document.createElement('div');
  16. while (e.firstChild) {
  17. div.appendChild(e.firstChild);
  18. }
  19. e.appendChild(div);
  20. }
  21. e=e.nextSibling;
  22. }
  23. e=m.getElementsByTagName('a');
  24. if (!mbTf) mbTf=new Function('mbHAT();');
  25. for (i=0;i<e.length;i++) {
  26. e[i].onmouseout=e[i].onblur=mbTf;
  27. }
  28. m=m.getElementsByTagName('ul');
  29. for (i=0;i<m.length;i++) {
  30. if (j=m[i].id) {
  31. mbO[j]=m[i];
  32. e=m[i].parentNode;
  33. f=e.parentNode.getAttribute('id');
  34. if (mbO[f]) {
  35. mbP[j]=f;
  36. }
  37. f=new Function('mbS(\''+j+'\');');
  38. e=e.getElementsByTagName('a');
  39. for (c=0;c<e.length;c++) {
  40. e[c].onmouseover=e[c].onfocus=f;
  41. }
  42. mbH(j);
  43. }
  44. }
  45. }}
  46. function mbHA() {
  47. if (mbA) {
  48. while (mbA) mbH(mbA);
  49. mbHE('visible');
  50. }
  51. }
  52. function mbHAT() {
  53. if (!mbT) mbT=setTimeout('mbHA();', mbW);
  54. }
  55. function mbTC() {
  56. if (mbT) {
  57. clearTimeout(mbT);
  58. mbT=null;
  59. }
  60. }
  61. function mbS(m) {
  62. mbTC();
  63. if (mbA) while (mbA&&m!=mbA&&mbP[m]!=mbA) mbH(mbA);
  64. else mbHE('hidden');
  65. mbSH(m,'visible');
  66. mbA=m;
  67. }
  68. function mbH(m) {
  69. if (m==mbA) mbA=mbP[m];
  70. mbSH(m,'hidden');
  71. mbT=null;
  72. }
  73. function mbSH(m,v) {
  74. mbO[m].parentNode.firstChild.className=v;
  75. mbO[m].style.visibility=v;
  76. }
  77. function mbHE(v) {
  78. mbHEV(v,document.getElementsByTagName('select'));
  79. }
  80. function mbHEV(v,e) {
  81. for (var i=0;i<e.length;i++) e[i].style.visibility=v;
  82. }

function mbSet(m, c)

Transforms <ul>elements into an application styles menu bar with popup sub-menus.

m
String: id of the <ul> to transform into a menu bar.
c
String: class name applied to menu bar.

function mbHA()

Hides all open menus on the page. This function is not 'per menubar' specific, as there should never be more than one menubar being accessed at any given moment.


function mbHAT()

Calls mbHA() after a timeout of mbW milliseconds.

This is triggered by an onblur or onmouseout event, and cancelled by a subsequent onfocus or onmouseover event. One of the tricks of UI programming ÃÆ’¢Ãƒ¢ÃƒÂ¢Ã¢â€šÂ¬Ã…¡Ã‚¬ÃƒÆ’‚¦ detecting when the menu has lost focus by tracking the individual components. There are doubtless more elegant solutions, but this serves well.


function mbTC()

Clears timeout set by mbHAT().


function mbS(m)

Shows a menu / submenu. Also toggles display of HTML elements which interfere with positioned menus.

m
String: id of the <ul> menu element to show.

function mbH(m)

Hide a menu / submenu.

m
String: id of the <ul> menu element to hide.

function mbSH(m,v)

Toggle between show/hide a menu.

m
String: id of the <ul> menu element to toggle.
v
String: visibility setting, either hidden or visible. Also used as a classname on the parent node's <a ÃÆ’¢Ãƒ¢ÃƒÂ¢Ã¢â€šÂ¬Ã…¡Ã‚¬ÃƒÆ’‚¦> tag.

function mbHE(v)

Some HTML elements conflict with the display of positioned elements. This functions finds all elements by tag name and toggles their visibility.

v
String: visibility setting, either hidden or visible.

function mbHEV(v,e)

Some HTML elements conflict with the display of positioned elements. This functions hides these elements whenever the menu is visible. It’s not a great solution, but it prevents them from obscuring menu items. Logically this is a CSS problem, but I have not yet found a CSS solution.

v
String: visibility setting, either hidden or visible. Also used as a classname on the parent node's <a ÃÆ’¢Ãƒ¢ÃƒÂ¢Ã¢â€šÂ¬Ã…¡Ã‚¬ÃƒÆ’‚¦> tag.
e[]
String: array of elements to toggle..
[ inspire logo ]
Code in the javascript library is free to use. Enjoy! Feedback welcome.
© Ben Boyle 2003.