...

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



briankohles
04-15-2005, 05:37 AM
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:



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



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:


while(target && target.nodeName.toUpperCase()!="DIV" && i++>100)

to this:


i++;

glenngv
04-15-2005, 05:52 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum