Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-23-2004, 03:58 PM   PM User | #1
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
Question missing results from while loop

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?
jbot is offline   Reply With Quote
Old 04-23-2004, 05:40 PM   PM User | #2
sad69
Senior Coder

 
Join Date: Feb 2004
Posts: 1,206
Thanks: 0
Thanked 0 Times in 0 Posts
sad69 is an unknown quantity at this point
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.
sad69 is offline   Reply With Quote
Old 04-26-2004, 09:28 AM   PM User | #3
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
Quote:
Originally Posted by sad69
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) .
jbot is offline   Reply With Quote
Old 04-26-2004, 09:52 AM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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?
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-26-2004, 09:54 AM   PM User | #5
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
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*
jbot is offline   Reply With Quote
Old 04-26-2004, 10:00 AM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
What browser are you testing it on? Can you post a link to your page?
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-26-2004, 10:05 AM   PM User | #7
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Have a look at the way you construct the while loop there.
Code:
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('_').
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-26-2004 at 10:12 AM..
liorean is offline   Reply With Quote
Old 04-26-2004, 10:07 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
I only noticed now, what's this?

return aTabIds, aClasses;

You can't return 2 values.

Testing it locally, the function only return the last array aClasses.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-26-2004, 10:13 AM   PM User | #9
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Quote:
Originally Posted by liorean
Have a look at the way you construct the while loop there.
Code:
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-26-2004, 10:15 AM   PM User | #10
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
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
Code:
return [aTabIds,aClasses];
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 04-26-2004 at 10:18 AM..
liorean is offline   Reply With Quote
Old 04-26-2004, 10:16 AM   PM User | #11
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
Quote:
Originally Posted by glenngv
return aTabIds, aClasses;

You can't return 2 values.
er, yes, you can. this has always worked for me.

Quote:
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
jbot is offline   Reply With Quote
Old 04-26-2004, 10:20 AM   PM User | #12
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally Posted by jbot
er, yes, you can. this has always worked for me.
It shouldn't have. What browser have it worked in?
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-26-2004, 10:26 AM   PM User | #13
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
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.
jbot is offline   Reply With Quote
Old 04-26-2004, 10:31 AM   PM User | #14
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Well, try running the following in respective browsers (from the location bar):
Code:
javascript:alert((function(x,y,z){return x,y,z;})(1,2,3));
It doesn't return 1,2,3 in any of them.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 04-26-2004, 10:42 AM   PM User | #15
jbot
Senior Coder

 
Join Date: Feb 2004
Location: Edinburgh
Posts: 1,352
Thanks: 0
Thanked 0 Times in 0 Posts
jbot is an unknown quantity at this point
no, you're right. but my script still works.

have simplified it now anyway, which works just fine:

Code:
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
jbot is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:21 PM.


Advertisement
Log in to turn off these ads.