[thelist] Friday Freebie

Scott Dexter sgd at ti3.com
Fri Jun 30 19:41:33 CDT 2000

<tip type="Javascript,Server side form generation">
So you have some server side processing that generates some form fields on
your html page. The thing is, depending on circumstances, the form may take
different shapes and some fields may or may not be there. Complicating
matters is your complex javascript routines that work on those form fields.

Obviously you'd like to keep the javascript re-writing to a minimum, and
there's no fun in writing logic on the server side to determine which set of
javascript to include.

Well, take a hint from our browser detection lesson, and check for the form
fields before you try working on them. This way you can write your
javascript once, and concentrate on those server side type-mismatch errors.

<form name="timesheet" action="..." ...>
<input type="text" name="fieldx" ...>

you can test for the existence of the field just as you can test for the
existence of any DOM object:

if (document.timesheet.fieldx) { //cool! its there!} else {//work around its

think safely

More information about the thelist mailing list