loop in javascript

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

    loop in javascript

    Hi!

    I have a menu-system in JavaScript.

    The menu is displayed by two js functions ( DrawMenuBar and DrawSubMenu ).
    the first function displays the parent items, and includes the other
    function to display sub items if it exists.
    I wanted to add one more child. that means the menu contained 3 levels.
    To display the third sub item, I made DrawSubMenu run a new
    function(DrawSu bSubMenu). That way the menu was able to have 3 levels.

    My question:
    If I wanted to make my menu lets say 10 levels..... Is it any good way to
    loop the function? Or is it best for me to just write a new subsubsub
    function and so on?
    One of the inportant things about the menu is hat it only expand's the item
    you have selected.(!)

    Thanks a lot in advance.

    best regards Christopher

    THIS IS MY CODE:

    ////////////////////////////////////////////////////////////////////////////
    ///////////////////
    function DrawMenuBar(oSi te, oRootNode, oRequestedNode)
    {
    if (oSite == null || oRootNode == null)
    return;

    var i=0;
    var oFirstLevelNode = null;

    Response.Write( "<TABLE BORDER=\"0\" CELLPADDING=\"0 \" CELLSPACING=\"0 \"
    WIDTH=\"140\">" );
    while ((oFirstLevelNo de = oRootNode.GetCh ildByOrder(i++) ) != null)
    {
    Response.Write( "<TR><TD CLASS=\"normal\ " HEIGHT=\"21\">" );

    var sImgNormal = oFirstLevelNode .GetImageMenuNo rmal();

    // Display images menu
    if (sImgNormal != "")
    {
    if (oFirstLevelNod e.IsLink())
    {
    Response.Write( "<A HREF=" + oSite.GetNodeLi nk(oFirstLevelN ode,true));

    if (oFirstLevelNod e.IsEqualOf(oRe questedNode) ||
    oFirstLevelNode .IsAncestorOf(o RequestedNode))
    Response.Write( "><IMG SRC=\"" + oFirstLevelNode .GetImageMenuAc tive()
    + "\"");
    else
    {
    Response.Write( " ONMOUSEOVER=\"d ocument.menu" + i + ".src='" +
    oFirstLevelNode .GetImageMenuMo useOver() + "'\"");
    Response.Write( " ONMOUSEOUT=\"do cument.menu" + i + ".src='" +
    oFirstLevelNode .GetImageMenuNo rmal() + "'\">");
    Response.Write( "<IMG NAME=\"menu" + i + "\" SRC=\"" +
    oFirstLevelNode .GetImageMenuNo rmal() + "\"");
    }

    Response.Write( " BORDER=\"0\" ALIGN=\"top\"></A>");
    }
    else
    Response.Write( "<IMG SRC=\"" + oFirstLevelNode .GetImageMenuNo rmal()
    +"\">");
    }
    // Display text menu
    else
    {
    if (oFirstLevelNod e.IsLink())
    {
    Response.Write( "&nbsp;<A HREF=" +
    oSite.GetNodeLi nk(oFirstLevelN ode,true) + ">");
    if (oFirstLevelNod e.IsEqualOf(oRe questedNode) ||
    oFirstLevelNode .IsAncestorOf(o RequestedNode))
    Response.Write( "<FONT COLOR=\"#F06B00 \"><B>" +
    oFirstLevelNode .GetTextMenu() + "</B></FONT>");
    else
    Response.Write( "<B>" + oFirstLevelNode .GetTextMenu() + "</B>");

    Response.Write( "</A>");
    }
    else
    Response.Write( "&nbsp;<B>" + oFirstLevelNode .GetTextMenu() + "</B>");
    }

    Response.Write( "</TD></TR>");

    // Display sub-menu for selected menu
    if (oFirstLevelNod e.HasChilds() &&
    (oFirstLevelNod e.IsAncestorOf( oRequestedNode) ||
    oFirstLevelNode .IsEqualOf(oReq uestedNode)))
    DrawSubMenu(oSi te, oFirstLevelNode , oRequestedNode) ;
    }

    Response.Write( "</TABLE>");
    }



    ////////////////////////////////////////////////////////////////////////////
    /////////////////
    function DrawSubMenu(oSi te, oNode, oRequestedNode)
    {
    if (oSite == null || oNode == null)
    return;

    // Display only text menu

    var oSecondLevelNod e = null;
    var i=0;

    Response.Write( "<TR><TD><I MG SRC=\"./images/1px_t.gif\" WIDTH=\"1\"
    HEIGHT=\"5\"></TD></TR>");

    while ((oSecondLevelN ode = oNode.GetChildB yOrder(i++)) != null)
    {
    Response.Write( "<TR><TD CLASS=\"normal\ " HEIGHT=\"21\">" );

    if (oSecondLevelNo de.IsLink() &&
    !oSecondLevelNo de.IsEqualOf(oR equestedNode))
    {
    Response.Write( "<IMG SRC=\"./images/1px_t.gif\" WIDTH=\"12\"
    HEIGHT=\"1\">") ;
    Response.Write( "<A HREF=" + oSite.GetNodeLi nk(oSecondLevel Node,true) +
    ">");
    Response.Write( oSecondLevelNod e.GetTextMenu() );
    Response.Write( "</A>");
    }
    else
    {
    Response.Write( "<IMG SRC=\"./images/arrow01.gif\">" );
    Response.Write( oSecondLevelNod e.GetTextMenu() );
    }

    Response.Write( "</TD></TR>");
    // Display subsub-menu for selected menu
    if (oSecondLevelNo de.HasChilds() &&
    (oSecondLevelNo de.IsAncestorOf (oRequestedNode ) ||
    oSecondLevelNod e.IsEqualOf(oRe questedNode)))
    DrawSubSubMenu( oSite, oSecondLevelNod e, oRequestedNode) ;
    }

    Response.Write( "<TR><TD><I MG SRC=\"./images/1px_t.gif\" WIDTH=\"1\"
    HEIGHT=\"10\"></TD></TR>");
    }


    ////////////////////////////////////////////////////////////////////////////
    ////////////////////
    function DrawSubSubMenu( oSite, oNode, oRequestedNode)
    {
    if (oSite == null || oNode == null)
    return;

    // Display only text menu

    var oThirdLevelNode = null;
    var i=0;

    Response.Write( "<TR><TD><I MG SRC=\"./images/1px_t.gif\" WIDTH=\"1\"
    HEIGHT=\"5\"></TD></TR>");

    while ((oThirdLevelNo de = oNode.GetChildB yOrder(i++)) != null)
    {
    Response.Write( "<TR><TD CLASS=\"normal\ " HEIGHT=\"21\">" );

    if (oThirdLevelNod e.IsLink() &&
    !oThirdLevelNod e.IsEqualOf(oRe questedNode))
    {
    Response.Write( "<IMG SRC=\"./images/1px_t.gif\" WIDTH=\"12\"
    HEIGHT=\"1\">") ;
    Response.Write( "<A HREF=" + oSite.GetNodeLi nk(oThirdLevelN ode,true) +
    ">");
    Response.Write( oThirdLevelNode .GetTextMenu()) ;
    Response.Write( "</A>");
    }
    else
    {
    Response.Write( "<IMG SRC=\"./images/arrow01.gif\">" );
    Response.Write( oThirdLevelNode .GetTextMenu()) ;
    }

    Response.Write( "</TD></TR>");
    }

    Response.Write( "<TR><TD><I MG SRC=\"./images/1px_t.gif\" WIDTH=\"1\"
    HEIGHT=\"10\"></TD></TR>");
    }



  • Dave Anderson

    #2
    Re: loop in javascript

    "Christophe r Brandsdal" wrote:[color=blue]
    >
    > I have a menu-system in JavaScript...
    >
    > ...If I wanted to make my menu lets say 10 levels..... Is
    > it any good way to loop the function? Or is it best for me
    > to just write a new subsubsub function and so on?
    > One of the inportant things about the menu is hat it only
    > expand's the item you have selected.(!)[/color]

    Menu/submenu systems are tree structures, and thus are well-suited for
    recursion. I would avoid loops entirely, and I would *definitely* use a
    single "function" (I would most likely use a constructor) for establishing
    all submenus.


    --
    Dave Anderson

    Unsolicited commercial email will be read at a cost of $500 per message. Use
    of this email address implies consent to these terms. Please do not contact
    me directly or ask me to contact you directly for assistance. If your
    question is worth asking, it's worth posting.


    Comment

    Working...