Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Adding an "alert" solves my problem?!
System
TS 6.5 SP1, Win 2k3
I have two drop-down lists, one which is static and the other which is dynamic and depends on the selected value of the first drop-down list. In typical fashion, when a user opens an existing DCR, I want the dynamic drop-down list's saved value to display properly. So, I'm calling the same function in my init() function and as an addItemHandler which builds the list of options according to what is currently selected in the static drop-down. If the DCR is new, no big deal -- nothing is populated. If it's an existing DCR, my code *should* cause the saved value to be saved by default.
However, it's always the first option which is selected by default and -- here's the weird part -- when I add an alert in the function, the proper option is selected.
The guts of my JS function is attached -- sorry it's attached but this POS DevNet software strips out my greater than and less than signs whether I put it in a CODE or HTML block or even if I leave it blank. Anyway, that's a separate gripe...
Dave
Find more posts tagged with
Comments
chuckles
I've actually had some similar experiences, but never quite put my finger on the source. The only thing I can say is that the problem is some sort of time delay. The alert() call lets the page catch up so that following line will work properly. I've found that by calling the offending line of code through a setTimeout() with a 500ms delay usually has the same effect. Where did you put the alert() in that code so it would work?
On an unrelated note, I see that you have a few eval statements in your code. Unlike Perl, eval() is not recommended for anything by a few very specific use cases in JavaScript. However, JavaScript, like Perl, supports arrays, associative arrays, nested data structures and anonymous constructors for all of them. There shouldn't be a need to use those evals and I wouldn't be surprised if they are slowing down your script a little.
Migrateduser
Chuckles,
I'm glad to know you saw the same thing... first, I try to avoid using "eval" but the dynamic drop-down list is based on a JS array, as you saw. This JS array is being composed elsewhere and ultimately comes from the database. So, I'm storing appropriate values in these arrays and need to reference them in this function. Since the specific array I use depends upon the item selected in the static array, I find myself with only this option. Besides, if it's on the client, as Javascript is, I'm not too concerned with causing a couple of lines to take more time and CPU than not.
Otherwise, I'm calling the ALERT just before the "new Option" statement and that causes it to work. I tried using setTimeout and thought that it might be a timing issue but when I was using it, I didn't know what function or statement I should put into the setTimeout so I think I just gave up on it.
What did you call in your setTimeout and what time delay helped?
Migrateduser
I was able to get this working and I wanted to share my solution --
Using setTimeout would have been a great idea except that I'm building a drop-down list and I'd need to pass an object into the function I'm running in setTimeout which is messy, etc. So, what I'm doing instead is actually so painfully simple, I'm kicking myself a little bit.
Referencing the code I pasted earlier in this thread, I included the following:
if (tmpVal) {
IWDatacapture.getItem('/ProjectItemType').setValue(tmpVal);
}
AFTER my setOptions statement. Important to note that tmpVal is declared earlier in the function and this is the value that was saved to the DCR previously.
So, if you've seen the code I pasted before, I actually DO need to call the "new Option" line with the appropriate true/false values. One would think this would be enough -- but I think there is a timing issue which is why I needed to include the IF statement above. Again, one would think that the IF statement would be enough but it's strange... when I use the IF statement without the appropriate true/false values in the "new Option" line, nothing is selected whatsoever.
I don't see this as a bug... just a Javascript quirk.