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.
Code:
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?
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*
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('_').
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.
Edit: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
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.
Quote:
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
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.