CodeSOD: Evaluating Selections
If you're writing an application with a drop-down list, it's typical and reasonable to auto-select a certain option in the list. But John found an approach to doing this that's anything but typical.
function selectsearch(myform, myselect, myinput) { var i = 0; var fvalue = 1; do { arrayresult = eval(myform+"."+myselect+".options["+i+"].value"); if (arrayresult == myinput) { eval(myform+"."+myselect+".options["+i+"].selected = true"); var fvalue=5; } i++; } while(fvalue<3);}
The core of this approach is a do/while loop, which is already an odd choice. But the loop itself is guarded not by i, our obvious counter variable, but by fvalue, which starts at 1 and causes the loop to exit when it exceeds 2. Which it only does when we've found the option we're looking for, which means this function will raise an error if it runs off the end of the array.
Which brings us to the evals. Instead of passing objects around, we pass strings around, using the DHTML style of accessing form fields wrapped up in a string-concatenated eval call.
If this code had a D&D alignment, it would be "chaotic eval".
[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!