eliminating a global variable

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

    eliminating a global variable

    I've created a site where the pages generally contain a table of
    contents (site map) down the left side. When each page loads, the
    first function is called. The second function populates a global var
    (array) of all the links.

    I try to avoid using global variables, but I am stumped how to
    eliminate this one (TocLinks) because other functions need to iterate
    through it. I think the answer is to create a custom object that the
    other functions can access. But I need some pointers on how to
    proceed.

    Here's the function that the pages call initially and the 2nd function
    that it calls:

    function SetCurrPgLink()
    {
    var parElem;
    var parElemUL;
    var findUL;

    GetPageLinks();

    for (var i = 0; i < TocLinks.length ; i++)
    {
    var x = TocLinks[i].href;
    if (x.indexOf(Curr entPage) > -1)
    {
    CurrentPgIndex = i;
    TocLinks[i].id = 'CurrentPgA';
    parElem = TocLinks[i].parentNode;
    parElem.style.b ackgroundColor = 'white';
    findUL = parElem.parentN ode.tagName.toL owerCase();
    if (findUL == 'ul')
    {
    parElemUL = parElem.parentN ode;
    parElemUL.style .display = 'block';
    }
    parElemUL.paren tNode.className = 'FolderOpenLI';
    break;
    }
    }
    }

    /* fine-tune list of anchors */
    function GetPageLinks()
    {
    var CountPgItems = 0;
    var TocDivNode = document.getEle mentById('TocDi v');
    var TocLinksAll = TocDivNode.getE lementsByTagNam e('a');

    for (var n = 0; n < TocLinksAll.len gth; n++)
    {
    if (TocLinksAll[n].parentNode.cla ssName == 'PageItem')
    {
    TocLinks[CountPgItems] = TocLinksAll[n];
    CountPgItems++;
    }
    }
    }
  • Michael Winter

    #2
    Re: eliminating a global variable

    On 17 Nov 2004 10:59:56 -0800, Jeff Gutsell <jeffgutsell@fu se.net> wrote:

    [snip]
    [color=blue]
    > I try to avoid using global variables, but I am stumped how to eliminate
    > this one (TocLinks) because other functions need to iterate through it.
    > I think the answer is to create a custom object that the other functions
    > can access. But I need some pointers on how to proceed.[/color]

    Nothing as complicated as that. Just return the array.

    function getPageLinks() {
    /* Using the document.links collection is potentially simpler
    * but it's based on the assumption that checking for PageItem
    * in the class attribute is sufficient to obtain the correct
    * links. If not, restore your original code.
    */
    var links = document.links, temp = [];
    for(var i = 0, n = links.length, c; i < n; ++i) {
    c = links[i];
    /* It may be more appropriate to use indexOf, rather than using
    * a normal comparison as the class attribute can contain more
    * than one class name as a space-separated list.
    */
    if(c.parentNode .className == 'PageItem') {
    temp[temp.length] = c;
    }
    }
    return temp;
    }


    var tocLinks = getPageLinks();

    [snip]
    [color=blue]
    > findUL = parElem.parentN ode.tagName.toL owerCase();
    > if (findUL == 'ul')[/color]

    The element name is always uppercase for HTML documents, and
    case-sensitive for XML-based documents. Assuming this is just HTML, it
    would be quicker to use

    if(parElem.pare ntNode.tagName == 'UL')

    and omit findUL entirely.

    [snip]

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    Working...