Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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) .

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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?

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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*

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    What browser are you testing it on? Can you post a link to your page?

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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('_').
    Last edited by liorean; 04-26-2004 at 10:12 AM.
    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

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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.

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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];
    Last edited by liorean; 04-26-2004 at 10:18 AM.
    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

  • #11
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    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

  • #12
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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

  • #13
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #14
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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

  • #15
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •