By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,629 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,629 IT Pros & Developers. It's quick & easy.

setting tab index

P: n/a
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>

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
>From: David McDivitt <x1*********@del-yahoo.com>
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.

Jul 23 '05 #2

P: n/a
David McDivitt wrote:
From: David McDivitt <x1*********@del-yahoo.com> <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.
Jul 23 '05 #3

P: n/a
>Subject: Re: setting tab index
Date: Fri, 8 Apr 2005 01:46:43 +0100

David McDivitt wrote:
From: David McDivitt <x1*********@del-yahoo.com><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.

Jul 23 '05 #4

P: n/a
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
Jul 23 '05 #5

P: n/a
>From: David McDivitt <x1*********@del-yahoo.com>
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 <x1*********@del-yahoo.com>

<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/de...sproglobal.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

Jul 23 '05 #6

P: n/a
>From: David McDivitt <x1*********@del-yahoo.com>
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 <x1*********@del-yahoo.com>

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

Jul 23 '05 #7

P: n/a
David McDivitt <x1*********@del-yahoo.com> 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]:[i]);
} 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8

P: n/a
>From: "Dietmar Meier" <us***************@innoline-systemtechnik.de>
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.

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.