setting tab index

D

David McDivitt

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>
 
D

David McDivitt

From: David McDivitt said:
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.
 
R

Richard Cornford

David said:
From: David McDivitt <[email protected]>
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
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.
 
D

David McDivitt

Subject: Re: setting tab index
Date: Fri, 8 Apr 2005 01:46:43 +0100

David said:
From: David McDivitt <[email protected]>
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
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.
 
D

David McDivitt

From: David McDivitt said:
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 said:
From: David McDivitt <[email protected]>
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
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
 
D

David McDivitt

From: David McDivitt said:
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 said:
From: David McDivitt <[email protected]>
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
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.
 
D

Dietmar Meier

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
 
L

Lasse Reichstein Nielsen

David McDivitt said:
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
 
D

David McDivitt

From: "Dietmar Meier said:
Subject: Re: setting tab index
Date: Fri, 8 Apr 2005 15:53:02 +0200




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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top