setting tab index

Discussion in 'Javascript' started by David McDivitt, Apr 7, 2005.

  1. I need to set tabs on java generated pages. Pages have four sections:
    header, sidebar, body, and footer. The sidebar and body change dynamically.
    The tab key must go to anchors, fields, and buttons doing all in the header
    first, all in the sidebar second, etc. A base page contains includes for all
    the pieces and has the body tag.

    I am trying to use code pasted below. Help would be appreciated. Thanks

    <script language="javascript">
    <!--
    tab = 0;
    if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
    return document.all[id];
    }
    }
    function setTabs (childObject) {
    for (j=0;j<childObject.childNodes.length;j++) {
    try {
    childObject.childNodes[j].tabIndex = tab;
    tab++;
    if
    (childObject.childNodes[j].childnodes.length > 0)
    setTabs(childObject.childNodes[j]); //should not have to check length before
    recurse
    }
    catch (e) {
    }
    }
    }
    function onLoadEvent() {
    setTabs(document.getElementById('divTopHeader'));
    setTabs(document.getElementById('divTopMenu'));
    setTabs(document.getElementById('divTopBody'));
    setTabs(document.getElementById('divTopFooter'));
    }
    //-->
    </script>
     
    David McDivitt, Apr 7, 2005
    #1
    1. Advertising

  2. >From: David McDivitt <>
    >Subject: setting tab index
    >Date: Thu, 07 Apr 2005 13:40:19 -0500
    >
    >I need to set tabs on java generated pages. Pages have four sections:
    >header, sidebar, body, and footer. The sidebar and body change dynamically.
    >The tab key must go to anchors, fields, and buttons doing all in the header
    >first, all in the sidebar second, etc. A base page contains includes for all
    >the pieces and has the body tag.
    >
    >I am trying to use code pasted below. Help would be appreciated. Thanks
    >
    ><script language="javascript">
    ><!--
    > tab = 0;
    > if(document.all && !document.getElementById) {
    > document.getElementById = function(id) {
    > return document.all[id];
    > }
    > }
    > function setTabs (childObject) {
    > for (j=0;j<childObject.childNodes.length;j++) {
    > try {
    > childObject.childNodes[j].tabIndex = tab;
    > tab++;
    > if
    >(childObject.childNodes[j].childnodes.length > 0)
    >setTabs(childObject.childNodes[j]); //should not have to check length before
    >recurse
    > }
    > catch (e) {
    > }
    > }
    > }
    > function onLoadEvent() {
    > setTabs(document.getElementById('divTopHeader'));
    > setTabs(document.getElementById('divTopMenu'));
    > setTabs(document.getElementById('divTopBody'));
    > setTabs(document.getElementById('divTopFooter'));
    > }
    >//-->
    ></script>


    When observing values it seemed stuff was not being saved on the stack when
    recursion was done. So in case not, I found the array push and pop
    instructions and used them to emulate a stack. The routine works excellent
    now. Code is pasted below.

    tab = 0;
    recurse = new Array();
    if(document.all && !document.getElementById) {
    document.getElementById = function(id) {
    return document.all[id];
    }
    }
    function setTabs (node) {
    for (j=0;j<node.childNodes.length;j++) {
    child = node.childNodes[j];
    if (child.nodeType==1) {
    if (child.nodeName=='A' ||
    child.onClick!=null) {
    try {
    child.tabIndex = tab;
    tab++;
    }
    catch (e) {
    }
    }
    if (child.childNodes.length>0) { //check not
    necessary but faster
    recurse.push(j);
    recurse.push(node);
    setTabs(child);
    node = recurse.pop();
    j = recurse.pop();
    }
    }
    }
    }
    function onLoadEvent() {
    setTabs(document.getElementById('divTopHeader'));
    setTabs(document.getElementById('divTopMenu'));
    setTabs(document.getElementById('divTopBody'));
    setTabs(document.getElementById('divTopFooter'));
    }

    A test will be done for push and pop methods. If not present, setTabs will
    exit at top.
     
    David McDivitt, Apr 8, 2005
    #2
    1. Advertising

  3. David McDivitt wrote:
    >>From: David McDivitt <>

    <snip>
    >> function setTabs (childObject) {
    >> for (j=0;j<childObject.childNodes.length;j++) {
    >> try {
    >> childObject.childNodes[j].tabIndex = tab;
    >> tab++;
    >> if
    >>(childObject.childNodes[j].childnodes.length > 0)
    >>setTabs(childObject.childNodes[j]); //should not have to
    >>check length before recurse

    <snip>
    > When observing values it seemed stuff was not being
    > saved on the stack when recursion was done.

    <snip>

    "Saved on the stack"? You are using a global variable - j - as a loop
    counter in a recursive function. Global variables are not contained by
    (restricted to) an execution context so they cannot be expected to be
    "saved on the stack" (in so far as javascript's stack of execution
    contexts can be regarded as a stack).

    Don't waste time, and clock cycles, implementing your own stack, just
    follow the general programming axiom that variables should never be
    given more scope than they absolutely need and use local variables.
    Learning to do so habitually will save you a lot of time and trouble in
    the long run.

    Richard.
     
    Richard Cornford, Apr 8, 2005
    #3
  4. >Subject: Re: setting tab index
    >Date: Fri, 8 Apr 2005 01:46:43 +0100
    >
    >David McDivitt wrote:
    >>>From: David McDivitt <>

    ><snip>
    >>> function setTabs (childObject) {
    >>> for (j=0;j<childObject.childNodes.length;j++) {
    >>> try {
    >>> childObject.childNodes[j].tabIndex = tab;
    >>> tab++;
    >>> if
    >>>(childObject.childNodes[j].childnodes.length > 0)
    >>>setTabs(childObject.childNodes[j]); //should not have to
    >>>check length before recurse

    ><snip>
    >> When observing values it seemed stuff was not being
    >> saved on the stack when recursion was done.

    ><snip>
    >
    >"Saved on the stack"? You are using a global variable - j - as a loop
    >counter in a recursive function. Global variables are not contained by
    >(restricted to) an execution context so they cannot be expected to be
    >"saved on the stack" (in so far as javascript's stack of execution
    >contexts can be regarded as a stack).
    >
    >Don't waste time, and clock cycles, implementing your own stack, just
    >follow the general programming axiom that variables should never be
    >given more scope than they absolutely need and use local variables.
    >Learning to do so habitually will save you a lot of time and trouble in
    >the long run.
    >
    >Richard.


    The variable j is used only within the setTabs function and is not mentioned
    outside that function. How does it therefore have a global scope? Everything
    having local scope within the function should go on the stack. That's the
    way other languages work. I read articles saying javascript does not put
    stuff on the stack very well when doing recursion, so your criticism is not
    well founded.
     
    David McDivitt, Apr 8, 2005
    #4
  5. >From: David McDivitt <>
    >Subject: Re: setting tab index
    >Date: Fri, 08 Apr 2005 08:15:20 -0500
    >
    >>Subject: Re: setting tab index
    >>Date: Fri, 8 Apr 2005 01:46:43 +0100
    >>
    >>David McDivitt wrote:
    >>>>From: David McDivitt <>

    >><snip>
    >>>> function setTabs (childObject) {
    >>>> for (j=0;j<childObject.childNodes.length;j++) {
    >>>> try {
    >>>> childObject.childNodes[j].tabIndex = tab;
    >>>> tab++;
    >>>> if
    >>>>(childObject.childNodes[j].childnodes.length > 0)
    >>>>setTabs(childObject.childNodes[j]); //should not have to
    >>>>check length before recurse

    >><snip>
    >>> When observing values it seemed stuff was not being
    >>> saved on the stack when recursion was done.

    >><snip>
    >>
    >>"Saved on the stack"? You are using a global variable - j - as a loop
    >>counter in a recursive function. Global variables are not contained by
    >>(restricted to) an execution context so they cannot be expected to be
    >>"saved on the stack" (in so far as javascript's stack of execution
    >>contexts can be regarded as a stack).
    >>
    >>Don't waste time, and clock cycles, implementing your own stack, just
    >>follow the general programming axiom that variables should never be
    >>given more scope than they absolutely need and use local variables.
    >>Learning to do so habitually will save you a lot of time and trouble in
    >>the long run.
    >>
    >>Richard.

    >
    >The variable j is used only within the setTabs function and is not mentioned
    >outside that function. How does it therefore have a global scope? Everything
    >having local scope within the function should go on the stack. That's the
    >way other languages work. I read articles saying javascript does not put
    >stuff on the stack very well when doing recursion, so your criticism is not
    >well founded.


    Reading Microsoft documentation at
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsproglobal.asp
    it says if the var statement is not used, the variable defaults to global
    scope. Your criticism may be correct, Richard. I will try using var instead.
    Thanks
     
    David McDivitt, Apr 8, 2005
    #5
  6. >From: David McDivitt <>
    >Subject: Re: setting tab index
    >Date: Fri, 08 Apr 2005 08:15:20 -0500
    >
    >>Subject: Re: setting tab index
    >>Date: Fri, 8 Apr 2005 01:46:43 +0100
    >>
    >>David McDivitt wrote:
    >>>>From: David McDivitt <>

    >><snip>
    >>>> function setTabs (childObject) {
    >>>> for (j=0;j<childObject.childNodes.length;j++) {
    >>>> try {
    >>>> childObject.childNodes[j].tabIndex = tab;
    >>>> tab++;
    >>>> if
    >>>>(childObject.childNodes[j].childnodes.length > 0)
    >>>>setTabs(childObject.childNodes[j]); //should not have to
    >>>>check length before recurse

    >><snip>
    >>> When observing values it seemed stuff was not being
    >>> saved on the stack when recursion was done.

    >><snip>
    >>
    >>"Saved on the stack"? You are using a global variable - j - as a loop
    >>counter in a recursive function. Global variables are not contained by
    >>(restricted to) an execution context so they cannot be expected to be
    >>"saved on the stack" (in so far as javascript's stack of execution
    >>contexts can be regarded as a stack).
    >>
    >>Don't waste time, and clock cycles, implementing your own stack, just
    >>follow the general programming axiom that variables should never be
    >>given more scope than they absolutely need and use local variables.
    >>Learning to do so habitually will save you a lot of time and trouble in
    >>the long run.
    >>
    >>Richard.

    >
    >The variable j is used only within the setTabs function and is not mentioned
    >outside that function. How does it therefore have a global scope? Everything
    >having local scope within the function should go on the stack. That's the
    >way other languages work. I read articles saying javascript does not put
    >stuff on the stack very well when doing recursion, so your criticism is not
    >well founded.


    No, it does not work. Using var does not cause a local variable to go on the
    stack if a function is recursed. Critical variables within the function must
    be pushed and popped from an array.
     
    David McDivitt, Apr 8, 2005
    #6
  7. David McDivitt wrote:

    >> You are using a global variable - j - as a loop counter in a
    >> recursive function.


    > The variable j is used only within the setTabs function and is not
    > mentioned outside that function. How does it therefore have a global
    > scope?


    The scope is not depending on where and how you use a variable, but
    on where and how you declare it. Compare

    function ...(...) {
    for (j=...; ...; ...) ...
    }

    with

    function ...(...) {
    for (var j=...; ...; ...) ...
    }

    or

    function ...(...) {
    var j, ...;
    for (j=...; ...; ...) ...
    }

    and read ECMA-262 section 12.2 if you don't see any differene there.

    ciao, dhgm
     
    Dietmar Meier, Apr 8, 2005
    #7
  8. David McDivitt <> writes:

    > No, it does not work. Using var does not cause a local variable to
    > go on the stack if a function is recursed.


    Yes it does. A local variable (either one declared with a "var"
    declaration inside the body of a function, or a parameter of the
    function) is local to one invocation. Recursion works perfectly
    fine.

    Try:
    ---
    function recurse(n, acc) {
    for(var i = 0; i < 2; i++) {
    if (n > 0) {
    recurse(n-1, acc?[i, acc]:);
    } else {
    alert([i,acc])
    }
    }
    }
    recurse(2);
    ---
    If your description was correct, this call would only alert
    0,0,0
    and
    1,0,0
    since "i" is a local (and critical) variable.

    Instead it alerts all 8 combinations as it should.

    > Critical variables within the function must be pushed and popped
    > from an array.


    No. Javascript handles local variables perfectly fine.

    If someone doesn't know the rules for when a variable is local, and
    uses a global variable in a recursive function, then obviosuly it
    doesn't work, but that's due to unfamiliarity with the language,
    not a problem with the language itself.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Apr 8, 2005
    #8
  9. >From: "Dietmar Meier" <>
    >Subject: Re: setting tab index
    >Date: Fri, 8 Apr 2005 15:53:02 +0200
    >
    >David McDivitt wrote:
    >
    >>> You are using a global variable - j - as a loop counter in a
    >>> recursive function.

    >
    >> The variable j is used only within the setTabs function and is not
    >> mentioned outside that function. How does it therefore have a global
    >> scope?

    >
    >The scope is not depending on where and how you use a variable, but
    >on where and how you declare it. Compare
    >
    > function ...(...) {
    > for (j=...; ...; ...) ...
    > }
    >
    >with
    >
    > function ...(...) {
    > for (var j=...; ...; ...) ...
    > }
    >
    >or
    >
    > function ...(...) {
    > var j, ...;
    > for (j=...; ...; ...) ...
    > }
    >
    >and read ECMA-262 section 12.2 if you don't see any differene there.
    >
    >ciao, dhgm



    Thanks everyone for the support. I have gotten a good education in
    javascript today. Local variables are being put on the stack correctly when
    I recurse a function and I have great happiness now.
     
    David McDivitt, Apr 8, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hermit Dave
    Replies:
    0
    Views:
    473
    Hermit Dave
    Jan 22, 2004
  2. News Sympatico
    Replies:
    0
    Views:
    328
    News Sympatico
    May 21, 2005
  3. tab 2 into tab 4 ?

    , Jun 19, 2005, in forum: Python
    Replies:
    2
    Views:
    354
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    355
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    212
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page