Re: Proper coding? (JS newbie)



Paul E. Schoen said the following on 9/4/2007 1:22 PM:
I recently made a simple on-line form with some JScript to serve as a foundation on which to build a more extensive application. It is basically a questionnaire that calculates a dog license fee based on weight and other factors. I have extensive programming experience in many languages, including C, Delphi, and PIC and Z80 assembly, but I am fairly new to JScript.

Go to www.getfirefox.com, download it, and try your page in it. The javascript console will help with a lot of the things in your script that are either wrong, bad, or could be done better.

My code was severely criticized by someone, and I want to understand why that might be. Please comment on the code I have posted on my website at:

http://www.smart.net/~pstech/DogLicenseJS.htm

First, don't use MSWord to create HTML documents. MSWord is a Word Processor, not an HTML editor.

These are the first few lines of your script block with a few comments about just that part of it:

<SCRIPT LANGUAGE="JavaScript"> //!-- Beginning of JavaScript -

Comment:Ditch the language attribute and use type="text/javascript" instead.


// JSTest.js - 08/26/07 - Paul E. Schoen
var InitialFee = 20;
var TotalFee = 20;
function ComputeTotal () {
TotalFee = InitialFee + 0.3*parseInt(Form1.Weight.value);

Form1.Weight.value is depending on an IE-ism of making ID/NAME attributes global variables. Better is the forms collection and referencing it as:
document.forms['formName'].elements['elementName'].value;

You will also run across people who say that naming functions beginning with an uppercase letter is a bad habit. That is something I disagree with as it doesn't matter but it's a heads up that people may say something about it.

A lot of the check boxes would make the script simpler if you used select elements. Or, in the script, use a case/switch construct.

I just want to be be sure that I'm not really on the wrong path if I decide to expand on this simple script.

Start with the comp.lang.javascript FAQ.
http://jibbering.com/faq/index.html

It is a very good starting place to learn some things to do, and some things not to do. It also has references that might help you with the language.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
.



Relevant Pages

  • Re: script doesnt work when run as cron job
    ... >> dress. ... I didn't consider referencing using an absolute path, ... >> What if they're related to a binary or shell script that resides in the ...
    (comp.unix.shell)
  • Saving figure file name as a function of input argument
    ... I'm having trouble with referencing values, matrices and strings in m files. ... My script is below. ...
    (comp.soft-sys.matlab)
  • Debugging WSF file that includes multiple vbs files in VS.NET 2003
    ... I'm attempting to make a script more maintainable by separating parts of the ... script into separate VBS files and then referencing them at the beginning of ... run in the debugger by using the //X option, ...
    (microsoft.public.scripting.wsh)
  • Re: why are references so slow ?
    ... there are still lot's of inefficient things in my script, ... that the slowest part's are actually really referencing and ... The costs of publication of this article were defrayed in part by the ...
    (comp.lang.perl.misc)
  • Re: Array Question
    ... not a programmer by any means. ... What this script does, ... complete the backup. ... multidimensional arrays when referencing static data. ...
    (microsoft.public.scripting.vbscript)

Loading