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.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript syntax error (fixed: but can anyone explain why)

    OK, so I snagged a small bit of code from a page & I'm trying to make it work in my situation (just as a test page for now). But the problem is I can't get the code to work in Firefox unless I add an alert() into it (however the alert box never shows up. With the alert in the code works in FF but not in IE, with the alert out the code works in IE but not FF. I just can't figure out where the problem is. The code for the function is below. When run in FF with the alert commented out I receive this error:

    Code:
    Error: target.style has no properties
    Line: 21
    The alert() which makes it all work or not is on line 15.

    Any help would be appreciated.
    Thanks
    Brian

    Code:
    	function toggleDisplay(aEvent) {
    		var target;
    		
    		if("fromElement" in aEvent){
    			target=aEvent.srcElement.nextSibling;
    		} else if("target" in aEvent) {
    			target=aEvent.target.nextSibling;
    		}
    		var i=0;
    		while(target && target.nodeName.toUpperCase()!="DIV" && i++>100)
    		//	alert("test");
    			target=target.nextSibling;
    		
    		if(!target || i>100) {
    			return;
    		}
    		var visible=target.style.display;
    		target.style.display=(!visible || visible=="block")? "none":"block";
    	}
    Edit: OK with a little more playing I got this to work. But I don't understand why my "fix" works.

    all I did was change this:
    Code:
    while(target && target.nodeName.toUpperCase()!="DIV" && i++>100)
    to this:
    Code:
    i++;
    Last edited by briankohles; 04-15-2005 at 06:33 AM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    When you put the alert, did you insert curly braces {} ?

    while(target && target.nodeName.toUpperCase()!="DIV" && i++>100) {
    alert("test");
    target=target.nextSibling;
    }

    Because if you did not, then the line "target=target.nextSibling;" will be executed only once because it is outside the loop.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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