Script works only when alert is called

I use script described here for making dynamic selectboxes (user selects something in the first box, then the options in the second box change automatically).

There was tiny problem - after form submitting was selected first option in right selectmenu (although there was selected=“selected” on other option). I fixed it almost , now it works with Firefox and Opera 8, but i noticed that with IE 6 it works only when there was called alert() function :confused:

Examples (use IE for testing):
with alert (works)

without alert (doesn’t work)

Here is commented source of this javascript file

What may be solution for this strange problem?

The “non-working” example works for me using IE 6 Windows OS with javascript version 1.3 support

Usually when something works with an alert but not without, it’s a timing issue (like the script is being called before the page finishes loading).

To fix this normally put function calls in the window.onload event handler.

I didn’t look so I don’t know if that’s the problem, but it usually is :slight_smile:

I tested with 3 computers and all failed (all OSes was windows xp (sp1, sp2 and no sp) with IE v6)… was there selected “#estonian” on right dropdown menu?

Yes, there is already called window.onload:

window.onload = function() {
	dynamicSelect("srv", "chn");
}

and this works too (it removes all options and adds what necessary) but it doesnt highlight selected as needed.

Sorry, I misunderstood the problem. I thought you meant the channel select didn’t change with changes to the server select.
So, no, it doesn’t work in IE.
I’m curious, if you temporarily hard code the alert (instead of having PHP conditionally echo it), is the behaviour the same? (my guess is yes, I’m just curious)

if(clonedOptions[i].value==selVal)
            {
                alert(selVal);
                sel2.value = selVal;
            }

I don’t know if it might be related to the IE appendChild bug
http://www.dynamicdrive.com/forums/showthread.php?t=5960
where the poster had to use a text node.
Or maybe it has something to do with IE and the “clones”.

Yes, its same, at the beginning i added it without php…

I tried to add these options with createTextNode like in this thread You linked, but unfortunately this works similary (only with alert)…

It must have something to do with the way browsers handle clones, memory, page rendering and onload events.
I notice that “estonian” doesn’t happen until after the alert is closed.
Currently the alert is accessing a DOM value. Does it still behave the same if you simply alert text? eg

 alert("show estonian!");

I added this selVal into setTimeout

if(clonedOptions[i].value==selVal)
{
	setTimeout(function(){
		sel2.value = selVal;
}, 500);
}

and seems that now it works :slight_smile:

Thanks.

Well pragmatically that is a good solution. Whether it’s a matter of loading or memory threading or whatever, we may never know. But until (if ever) IE gets up to standards, it’s good to find work-arounds, no matter how cludgy. Thank you for posting your fix.