[Javascript] Best Practices

Paul Novitski paul at juniperwebcraft.com
Fri Apr 14 11:12:49 CDT 2006


At 12:42 PM 4/13/2006, Triche Osborne wrote:
>I'm building an adaptable form for a scheduling application in which 
>the answer from a 3-5 button radio group determines whether or not 
>the JS generates a variable number of extra sets of compound date/time fields.
>         To do this, I can either hide a single invisible template, 
> then clone the template, change the field names and show the 
> resulting fields, or I can generate the extra sets "from scratch."
>         Is either method preferred? *


Triche, my sense is that the Best Practices in this case are:

1) Set up your application to run perfectly in the absence of 
DOM-aware JavaScript: when selections are made, post the page back to 
the server which returns it with the correct number of input fields 
built into the page.

2) If JavaScript is enabled and sufficiently modern to support the 
DOM, produce the desired fields on the fly to obviate the need for a 
server round-trip.

To address your specific question, my own preference would be to 
clone an existing template rather than building element from scratch 
because cloning allows for better separation of markup from 
logic.  You can tweak the HTML template without necessarily having to 
rewrite the script, and in fact you can potentially repurpose the 
cloning script broadly.

I also clone when delivering variable markup from a server-side 
script; in fact both server-side and client-side scripts can utilize 
the same markup template, making for much easier maintenance in the future.

Regards,
Paul 




More information about the Javascript mailing list