...

View Full Version : missing results from while loop



jbot
04-23-2004, 04:58 PM
got this weird error (save that there's no actual JS error going on), whereby my while loop initialises two arrays with values. when alerted outside of the loop, nuffin happens.

can anyone point out to me what might actually be happening, thanks.


function getTabs2Save()
{
var oDiv, iKey, i=0;

aTabIds=[];
aClasses=[];

while (iKey = (oDiv = document.getElementsByTagName("div").item(i++)).className.lastIndexOf("_"))
{
if (iKey != -1)
{
if (oDiv.className.substring(0, iKey).toLowerCase() == "tab")
{
aTabIds.push(oDiv.id);
aClasses.push(oDiv.className);

alert("details:\n---------\n\n" +aTabIds+ "\n" + aClasses) ;
}
}
}

alert("tabs ids: " + aTabIds) ;

return aTabIds, aClasses;
}


the first alert shows data, but the next one doesn't. in fact, the 2nd alert doesn't even get called?? what's going on with that? :confused:

sad69
04-23-2004, 06:40 PM
I'm not sure I see your conditional statement in your while clause..

I'm guessing one of those = should be an == (assignment vs. equality). That's probably one problem right there.

If that doesn't fix it, post back and I'll take a second look.

Sadiq.

jbot
04-26-2004, 10:28 AM
I'm guessing one of those = should be an == (assignment vs. equality). That's probably one problem right there.


nope, both are meant as assignments. if you look closely at the parenthesis you'll see that:

iKey = (oDiv = document.getElementsByTagName("div").item(i++)).className.lastIndexOf("_")

the loop does work: it does create the values I want. but the script never seems to get to alert("tabs ids: " + aTabIds) .

glenngv
04-26-2004, 10:52 AM
Maybe there is a run-time error inside the loop (in a particular iteration), that's why the 2nd alert is never executed. Do you get any error message?

jbot
04-26-2004, 10:54 AM
don't think it does. nuffin seems to happen at all.

it goes on with the rest of the script on the page, at the least the functions which call this one. it just doesn't call the 2nd alert or return the array values, despite setting them inside the loop. *scratches head*

glenngv
04-26-2004, 11:00 AM
What browser are you testing it on? Can you post a link to your page?

liorean
04-26-2004, 11:05 AM
Have a look at the way you construct the while loop there.

iKey=
(oDiv=document.getElementsByTagName("div").item(i++))
.className.lastIndexOf("_")
So, okay, oDiv will have a value of [object HTMLDivElement] for each iteration until you run out of div elements, at which time it will be null. And since null evaluates to false, it would nicely stop the loop if that was the entire expression in the while argument. However, you're reading out oDiv.className.lastIndexOf('_') inside the argument, something that may evaluate to false if it happens to return 0. However, null doesn't have any properties. That's the bug. You can't read out null.className.lastIndexOf('_').

glenngv
04-26-2004, 11:07 AM
I only noticed now, what's this? :eek:

return aTabIds, aClasses;

You can't return 2 values.

Testing it locally, the function only return the last array aClasses.

glenngv
04-26-2004, 11:13 AM
Have a look at the way you construct the while loop there.

iKey = (oDiv = document.getElementsByTagName("div").item(i++)).className.lastIndexOf("_")
So, okay, oDiv will have a value of [object HTMLDivElement] for each iteration until you run out of div elements, at which time it will be null. However, null doesn't have any properties. That's the bug.

Oh, that's right. It should throw a "oDiv is null or undefined/does not have any properties" error. It's weird you didn't catch that.

liorean
04-26-2004, 11:15 AM
Glenn's last post wasn't there when I posted this one...
Yeah, Glenn's right about that one. The comma operator is sequential, all expressions are evaluated in order but only the last value is returned. In most other cases you'd be required to surround the expression with parens to get the comma operator to work, though - I'm a bit surprised it works in the return statement.

Anyway, that's easily fixed, I guess what you really wanted would be
return [aTabIds,aClasses];

jbot
04-26-2004, 11:16 AM
return aTabIds, aClasses;

You can't return 2 values.


er, yes, you can. this has always worked for me.


What browser are you testing it on? Can you post a link to your page?

sorry, can't do that. working on an application under JBoss/localhost. Test browser's are IE6 and Firefox, and the HTML the JSP produces is almost unreadable without a lot of reformatting and time.


you're reading out oDiv.className.lastIndexOf('_') inside the argument, something that may evaluate to false if it happens to return 0. However, null doesn't have any properties. That's the bug. You can't read out null.className.lastIndexOf('_')

thanks for that. I kinda suspected something like that myself, but just couldn't figure out the precise cause. cheers :thumbsup:

liorean
04-26-2004, 11:20 AM
er, yes, you can. this has always worked for me.
It shouldn't have. What browser have it worked in?

jbot
04-26-2004, 11:26 AM
it's worked for me in every browser from NN4.08 right up to Firefox0.8 and IE6.

i'm not kidding. i understand that it probably shouldn't. that i should, as in PHP, return an array of values, that get's "listed" at the other end, but I've never needed to do that.

liorean
04-26-2004, 11:31 AM
Well, try running the following in respective browsers (from the location bar):
javascript:alert((function(x,y,z){return x,y,z;})(1,2,3));It doesn't return 1,2,3 in any of them.

jbot
04-26-2004, 11:42 AM
no, you're right. but my script still works.

have simplified it now anyway, which works just fine:



while (oDiv = document.getElementsByTagName("div").item(i++))
{
if (typeof(oDiv)!=null)
{
var iKey = oDiv.className.lastIndexOf("_") ;

if (iKey != -1)
{
var sPfx = oDiv.className.substring(0, iKey).toLowerCase() ;

if (sPfx == "tab")
{
aBoxIds.push(oDiv.id);
aBoxClss.push(oDiv.className);
}
}
}
}

alert("tabs ids: " + aBoxIds) ;


thanks again, guys :thumbsup:

jbot
04-26-2004, 11:53 AM
actually, i have the returning variables set up as globals. that'll be why i'm able to access them in other functions. :p

glenngv
04-26-2004, 12:00 PM
no, you're right. but my script still works.

have simplified it now anyway, which works just fine:



while (oDiv = document.getElementsByTagName("div").item(i++))
{
if (typeof(oDiv)!=null)
{
var iKey = oDiv.className.lastIndexOf("_") ;

if (iKey != -1)
{
var sPfx = oDiv.className.substring(0, iKey).toLowerCase() ;

if (sPfx == "tab")
{
aBoxIds.push(oDiv.id);
aBoxClss.push(oDiv.className);
}
}
}
}

alert("tabs ids: " + aBoxIds) ;


thanks again, guys :thumbsup:
The line

if (typeof(oDiv)!=null)

is not needed anymore since when oDiv is null, it won't pass through the while-loop.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum