Javascript menu modifications

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    Javascript menu modifications

    I've been using Travis's Expanding Menu
    (http://www.squidfingers.com/code/dhtml/expandingmenu/) for a site i'm
    working on. The problem is I need to add some functionality to it.

    I need the the ability to define a subsection to be open when you land
    on a page, but when you select another option for it to open that
    section and close the previously open one.

    I though i'd got round this by adding a class to the nested ol and
    setting it to display: inline; in the CSS, but unfortunatly, because
    the CSS forces the nested section open it remains open when you click
    another section.

    Any javascript experts out there fancy helping me out? Code posted
    below...


    // Node Functions

    if(!window.Node ){
    var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
    }
    function checkNode(node, filter){
    return (filter == null || node.nodeType == Node[filter] ||
    node.nodeName.t oUpperCase() == filter.toUpperC ase());
    }
    function getChildren(nod e, filter){
    var result = new Array();
    var children = node.childNodes ;
    for(var i = 0; i < children.length ; i++){
    if(checkNode(ch ildren[i], filter))
    result[result.length] = children[i];
    }
    return result;
    }
    function getChildrenByEl ement(node){
    return getChildren(nod e, "ELEMENT_NODE") ;
    }
    function getFirstChild(n ode, filter){
    var child;
    var children = node.childNodes ;
    for(var i = 0; i < children.length ; i++){
    child = children[i];
    if(checkNode(ch ild, filter)) return child;
    }
    return null;
    }
    function getFirstChildBy Text(node){
    return getFirstChild(n ode, "TEXT_NODE" );
    }
    function getNextSibling( node, filter){
    for(var sibling = node.nextSiblin g; sibling != null; sibling =
    sibling.nextSib ling){
    if(checkNode(si bling, filter)) return sibling;
    }
    return null;
    }
    function getNextSiblingB yElement(node){
    return getNextSibling( node, "ELEMENT_NODE") ;
    }

    // Menu Functions & Properties

    var activeMenu = null;

    function showMenu(){
    if(activeMenu){
    activeMenu.clas sName = "";
    getNextSiblingB yElement(active Menu).style.dis play =
    "none";
    }
    if(this == activeMenu){
    activeMenu = null;
    }else{
    this.className = "active";
    getNextSiblingB yElement(this). style.display = "block";
    activeMenu = this;
    }
    return false;
    }
    function initMenu(){
    var menus, menu, text, a, i;
    menus = getChildrenByEl ement(document. getElementById( "menu"));
    for(i = 0; i < menus.length; i++){
    menu = menus[i];
    text = getFirstChildBy Text(menu);
    a = document.create Element("a");
    menu.replaceChi ld(a, text);
    a.appendChild(t ext);
    a.href = "#";
    a.onclick = showMenu;
    a.onfocus = function(){this .blur()};
    }
    }

    // ||||||||||||||| ||||||||||||||| ||||||||||||||| |||||

    if(document.cre ateElement) window.onload = initMenu;

  • Stephen Chalmers

    #2
    Re: Javascript menu modifications



    Jay <jvbates@hotmai l.com> wrote in message
    news:1108126535 .954571.244000@ l41g2000cwc.goo glegroups.com.. .[color=blue]
    > I've been using Travis's Expanding Menu
    > (http://www.squidfingers.com/code/dhtml/expandingmenu/) for a site i'm
    > working on. The problem is I need to add some functionality to it.
    >
    > I need the the ability to define a subsection to be open when you land
    > on a page, but when you select another option for it to open that
    > section and close the previously open one.
    >
    > I though i'd got round this by adding a class to the nested ol and
    > setting it to display: inline; in the CSS, but unfortunatly, because
    > the CSS forces the nested section open it remains open when you click
    > another section.
    >
    > Any javascript experts out there fancy helping me out?[/color]

    Here's a kludge that seems to work. Replace initMenu() with this code, and
    set 'startWith' to the section you want to display initially, where 0 ==
    first.

    function initMenu()
    {
    var menus, menu, text, a, i, startWith=1; // Sets initially displayed
    section
    menus = getChildrenByEl ement(document. getElementById( "menu"));
    for(i = 0; i < menus.length; i++)
    {
    menu = menus[i];
    text = getFirstChildBy Text(menu);
    a = document.create Element("a");

    menu.replaceChi ld(a, text);
    a.appendChild(t ext);
    a.href = "#";
    a.onclick = showMenu;
    a.onfocus = function(){this .blur()};
    a.id="mLink"+i
    if(i==startWith )

    getNextSiblingB yElement((activ eMenu=document. getElementById( a.id))).style.d i
    splay = "block";
    }

    }

    --
    S.C. http://makeashorterlink.com/?H3E82245A



    Comment

    • Jay

      #3
      Re: Javascript menu modifications

      > Here's a kludge that seems to work. Replace initMenu() with this
      code, and[color=blue]
      > set 'startWith' to the section you want to display initially, where 0[/color]
      ==[color=blue]
      > first.
      >
      > function initMenu()
      > {
      > var menus, menu, text, a, i, startWith=1; // Sets initially[/color]
      displayed[color=blue]
      > section
      > menus = getChildrenByEl ement(document. getElementById( "menu"));
      > for(i = 0; i < menus.length; i++)
      > {
      > menu = menus[i];
      > text = getFirstChildBy Text(menu);
      > a = document.create Element("a");
      >
      > menu.replaceChi ld(a, text);
      > a.appendChild(t ext);
      > a.href = "#";
      > a.onclick = showMenu;
      > a.onfocus = function(){this .blur()};
      > a.id="mLink"+i
      > if(i==startWith )
      >
      >[/color]
      getNextSiblingB yElement((activ eMenu=document. getElementById( a.id))).style.d i[color=blue]
      > splay = "block";
      > }
      >
      > }[/color]

      Stephen,

      thanks for taking the time to help me out with this one, unfortunatly
      my knowledge of javascript is rubbish, hense my asking for some help.
      Your 'kludge' as you put it works great so thanks in advance! Although
      I have a couple of other questions,.. is there an easy way to add these
      two features...

      (1) The script is currently, held in an external javascript document,
      so is there any way of putting the 'StartWith' trigger onto the page?

      (2) Also in some cases the menu needs to display as default i.e with no
      sub menu displayed.

      If you (or anyone else) could spare the time to have a look at this
      then I would be most grateful!

      James

      Comment

      • Stephen Chalmers

        #4
        Re: Javascript menu modifications



        Jay <jvbates@hotmai l.com> wrote in message
        news:1108375966 .434748.167230@ g14g2000cwa.goo glegroups.com.. .[color=blue][color=green]
        > > Here's a kludge that seems to work. Replace initMenu() with this[/color]
        > code, and[color=green]
        > > set 'startWith' to the section you want to display initially, where 0[/color]
        > ==[color=green]
        > > first.
        > >
        > > function initMenu()
        > > {
        > > var menus, menu, text, a, i, startWith=1; // Sets initially[/color]
        > displayed[color=green]
        > > section
        > > menus = getChildrenByEl ement(document. getElementById( "menu"));
        > > for(i = 0; i < menus.length; i++)
        > > {
        > > menu = menus[i];
        > > text = getFirstChildBy Text(menu);
        > > a = document.create Element("a");
        > >
        > > menu.replaceChi ld(a, text);
        > > a.appendChild(t ext);
        > > a.href = "#";
        > > a.onclick = showMenu;
        > > a.onfocus = function(){this .blur()};
        > > a.id="mLink"+i
        > > if(i==startWith )
        > >
        > >[/color]
        >[/color]
        getNextSiblingB yElement((activ eMenu=document. getElementById( a.id))).style.d i[color=blue][color=green]
        > > splay = "block";
        > > }
        > >
        > > }[/color]
        >
        > Stephen,
        >
        > thanks for taking the time to help me out with this one, unfortunatly
        > my knowledge of javascript is rubbish, hense my asking for some help.
        > Your 'kludge' as you put it works great so thanks in advance! Although
        > I have a couple of other questions,.. is there an easy way to add these
        > two features...
        >
        > (1) The script is currently, held in an external javascript document,
        > so is there any way of putting the 'StartWith' trigger onto the page?[/color]

        Delete the declaration of startWith in the initMenu() function, then define
        startWith within script tags in the page containing the menu:

        <script type='text/javascript'>
        var startWith=1;
        </script>

        [color=blue]
        > (2) Also in some cases the menu needs to display as default i.e with no
        > sub menu displayed.[/color]

        Then under those circumstances, assign startWith a value of -1.

        --
        S.C. http://makeashorterlink.com/?H3E82245A



        Comment

        • RobB

          #5
          Re: Javascript menu modifications

          Stephen Chalmers wrote:[color=blue]
          > Jay <jvbates@hotmai l.com> wrote in message
          > news:1108375966 .434748.167230@ g14g2000cwa.goo glegroups.com.. .[color=green][color=darkred]
          > > > Here's a kludge that seems to work. Replace initMenu() with this[/color]
          > > code, and[color=darkred]
          > > > set 'startWith' to the section you want to display initially,[/color][/color][/color]
          where 0[color=blue][color=green]
          > > ==[color=darkred]
          > > > first.
          > > >
          > > > function initMenu()
          > > > {
          > > > var menus, menu, text, a, i, startWith=1; // Sets initially[/color]
          > > displayed[color=darkred]
          > > > section
          > > > menus = getChildrenByEl ement(document. getElementById( "menu"));
          > > > for(i = 0; i < menus.length; i++)
          > > > {
          > > > menu = menus[i];
          > > > text = getFirstChildBy Text(menu);
          > > > a = document.create Element("a");
          > > >
          > > > menu.replaceChi ld(a, text);
          > > > a.appendChild(t ext);
          > > > a.href = "#";
          > > > a.onclick = showMenu;
          > > > a.onfocus = function(){this .blur()};
          > > > a.id="mLink"+i
          > > > if(i==startWith )
          > > >
          > > >[/color]
          > >[/color]
          >[/color]
          getNextSiblingB yElement((activ eMenu=document. getElementById( a.id))).style.d i[color=blue][color=green][color=darkred]
          > > > splay = "block";
          > > > }
          > > >
          > > > }[/color]
          > >
          > > Stephen,
          > >
          > > thanks for taking the time to help me out with this one,[/color][/color]
          unfortunatly[color=blue][color=green]
          > > my knowledge of javascript is rubbish, hense my asking for some[/color][/color]
          help.[color=blue][color=green]
          > > Your 'kludge' as you put it works great so thanks in advance![/color][/color]
          Although[color=blue][color=green]
          > > I have a couple of other questions,.. is there an easy way to add[/color][/color]
          these[color=blue][color=green]
          > > two features...
          > >
          > > (1) The script is currently, held in an external javascript[/color][/color]
          document,[color=blue][color=green]
          > > so is there any way of putting the 'StartWith' trigger onto the[/color][/color]
          page?[color=blue]
          >
          > Delete the declaration of startWith in the initMenu() function, then[/color]
          define[color=blue]
          > startWith within script tags in the page containing the menu:
          >
          > <script type='text/javascript'>
          > var startWith=1;
          > </script>
          >
          >[color=green]
          > > (2) Also in some cases the menu needs to display as default i.e[/color][/color]
          with no[color=blue][color=green]
          > > sub menu displayed.[/color]
          >
          > Then under those circumstances, assign startWith a value of -1.
          >
          > --
          > S.C. http://makeashorterlink.com/?H3E82245A[/color]

          Or...

          function initMenu(item)
          {
          var menus, menu, text, a, i;
          if (item) item = new RegExp(item);
          menus = getChildrenByEl ement(document. getElementById( "menu"));
          for(i = 0; i < menus.length; i++)
          {
          menu = menus[i];
          text = getFirstChildBy Text(menu);
          a = document.create Element("a");
          menu.replaceChi ld(a, text);
          a.appendChild(t ext);
          a.href = "#";
          a.onclick = showMenu;
          a.onfocus = function(){this .blur()};
          if (item && item.test(text. nodeValue))
          a.onclick();
          }
          }

          ...and in specific pages:

          if(document.cre ateElement)
          window.onload = function()
          {
          initMenu('Menu Item 3');
          }

          Comment

          • James Bates

            #6
            Re: Javascript menu modifications

            Stephen,

            I have been unable to get your last chunk of code to work.. it maybe
            that i've
            implemented it wrong or something the actual page is here..

            Page with your code


            External javascript


            Any ideas?

            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • James Bates

              #7
              Re: Javascript menu modifications

              Stephen,

              Hope this doesn't double post (as the last one seemed not to work!)

              I have tried to implement your last chunk of code, but it doesn't seem
              to
              work, this may be due to me implementing it wrong!

              Page here...


              External JS here...


              Any ideas?

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • James Bates

                #8
                Re: Javascript menu modifications

                Stephen,

                you still about, any chance of helping me finish this one?

                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                • RobB

                  #9
                  Re: Javascript menu modifications

                  James Bates wrote:[color=blue]
                  > Stephen,
                  >
                  > you still about, any chance of helping me finish this one?
                  >
                  > *** Sent via Developersdex http://www.developersdex.com ***
                  > Don't just participate in USENET...get rewarded for it![/color]

                  My name isn't 'James' but I took the time to look at your problem. Not
                  clear why you posted in a public forum...

                  Comment

                  • Jay

                    #10
                    Re: Javascript menu modifications

                    RobB,

                    I'm sorry what was the problem, with posting on this forum? I was hoping
                    to
                    get some help to finish my javascript problem, I though it would be
                    useful to
                    keep it within the thread incase someone else found it useful!

                    Did you have a look, at the code? Can you see what the problem might be
                    with the last bit of code?

                    *** Sent via Developersdex http://www.developersdex.com ***
                    Don't just participate in USENET...get rewarded for it!

                    Comment

                    • RobB

                      #11
                      Re: Javascript menu modifications

                      Jay wrote:[color=blue]
                      > RobB,
                      >
                      > I'm sorry what was the problem, with posting on this forum? I was[/color]
                      hoping[color=blue]
                      > to
                      > get some help to finish my javascript problem, I though it would be
                      > useful to
                      > keep it within the thread incase someone else found it useful!
                      >
                      > Did you have a look, at the code? Can you see what the problem might[/color]
                      be[color=blue]
                      > with the last bit of code?
                      >
                      > *** Sent via Developersdex http://www.developersdex.com ***
                      > Don't just participate in USENET...get rewarded for it![/color]

                      Jeez...my name is right there. Just look up. #:o

                      I posted an alternative. Would be glad to explain it but it's a bit
                      difficult if you can't see that earlier post.

                      Comment

                      • Jay

                        #12
                        Re: Javascript menu modifications

                        RobB

                        Sorry if I'm getting your username wrong!! All I get next to your posts
                        is..
                        From: RobB, Date Posted: 2/16/2005 2:33:00 PM

                        Where did you post that explanation, because I can't see it at all?!! is
                        there
                        any reason why that should be.

                        Are you viewing this thread from http://www.developersdex.com ? As I
                        notice
                        that these forums are fed to several sites!

                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        • Randy Webb

                          #13
                          Re: Javascript menu modifications

                          Jay wrote:[color=blue]
                          > RobB
                          >
                          > Sorry if I'm getting your username wrong!! All I get next to your posts
                          > is..
                          > From: RobB, Date Posted: 2/16/2005 2:33:00 PM
                          >
                          > Where did you post that explanation, because I can't see it at all?!! is
                          > there
                          > any reason why that should be.
                          >
                          > Are you viewing this thread from http://www.developersdex.com ? As I
                          > notice that these forums are fed to several sites![/color]

                          NO! This forum isn't fed to several sites, several sites hijack Usenet,
                          claim its a forum, and then unsuspecting people as yourself assume that
                          its a forum when it's not. Go to :
                          <URL:
                          http://groups-beta.google.com/group/...n&lr=&ie=UTF-8[color=blue]
                          >[/color]

                          And you will see the archives of comp.lang.javas cript, which is where
                          you are actually posting to, and you will undoubtedly find your posts there.

                          --
                          Randy
                          comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                          Comment

                          • RobG

                            #14
                            Re: Javascript menu modifications

                            Jay wrote:[color=blue]
                            > RobB
                            >
                            > Sorry if I'm getting your username wrong!! All I get next to your posts
                            > is..
                            > From: RobB, Date Posted: 2/16/2005 2:33:00 PM
                            >
                            > Where did you post that explanation, because I can't see it at all?!! is
                            > there
                            > any reason why that should be.[/color]

                            Make the following into a single line URL (cut 'n paste into say
                            notepad, remove new lines), then paste into your browser's
                            address bar to see the entire thread:


                            browse_frm/thread/1a55751b9239029 5/1e4744fd429479a 0?
                            tvc=1&q=Re:+Jav ascript+menu+mo difications&_do ne=%2Fgroup%2
                            Fcomp.lang.java script%2Fsearch %3Fgroup%3Dcomp .lang.javascrip t%26
                            q%3DRe:+Javascr ipt+menu+modifi cations%26qt_g% 3D1%26searchnow %3
                            DSearch+this+gr oup%26&_doneTit le=Back+to+Sear ch&scrollSave=& &
                            d#1e4744fd42947 9a0


                            It's broken 'cos many group hijackers simply trunkate long URLs.
                            Hopefully manual wrapping will avoid that...


                            --
                            Rob

                            Comment

                            • Rob B

                              #15
                              Re: Javascript menu modifications

                              Resubmitted:

                              Or...

                              function initMenu(item)
                              {
                              var menus, menu, text, a, i;
                              if (item) item = new RegExp(item);
                              menus = getChildrenByEl ement(document. getElementById( "menu"));
                              for(i = 0; i < menus.length; i++)
                              {
                              menu = menus[i];
                              text = getFirstChildBy Text(menu);
                              a = document.create Element("a");
                              menu.replaceChi ld(a, text);
                              a.appendChild(t ext);
                              a.href = "#";
                              a.onclick = showMenu;
                              a.onfocus = function(){this .blur()};
                              if (item && item.test(text. nodeValue))
                              a.onclick();
                              }
                              }

                              ...and in specific pages:

                              if(document.cre ateElement)
                              window.onload = function()
                              {
                              initMenu('Menu Item 3');
                              }

                              *** Sent via Developersdex http://www.developersdex.com ***
                              Don't just participate in USENET...get rewarded for it!

                              Comment

                              Working...