Re: Proper coding? (JS newbie)




"Randy Webb" <HikksNotAtHome@xxxxxxx> wrote in message
news:WbidnSa9h9mCekDbRVn_vw@xxxxxxxxxxxxxxx
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.

I've been just running it on IE, but the error messages are not helpful.
Hopefully Firefox will show the errors more clearly.


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.

Yes, I started the web page using MS Word, and then found that it actually
removed the script when viewing the source. I am now using Arachnophilia,
but it does not have a good way to see the results.

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;

That should be very helpful, especially if I want to reference the elements
as an array, which was suggested.


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.

I'm used to Delphi, which is not case sensitive, but variable names and
functions usually start with an uppercase. Sometimes a lowercase letter is
prepended indicating variable type (like nCount for integer or sName for
string). I find it difficult not to predefine all the variable types, and
I've been stung a few times in JScript, where case matters.


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.

In my book, "Using JScript" (c1997), it shows the case/switch as a Java
Language construct, along with other distinctions from JScript. I probably
need a newer book!

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.

Many thanks for your helpful comments. I'm not doing any serious projects
right now, but I want to get some experience and instruction in the proper
way to use the language.

Paul


.



Relevant Pages

  • Re: Which Is The Better Approach To Working With Javascript?
    ... Java SCRIPT runs in the browser exclusively. ... No language is written just for a single environment. ... You have to understand that I come from a hardware background, a huge area that is complately unknown to most 'computer programmers' whose life begins and ends with an interpreter or compiler.. ...
    (comp.lang.php)
  • Re: Newbie
    ... The "=" was highlighted on the second line of script, ... I searched the Help for "VBScript" and found that it is somehow ... scripting language than VBA. ... used by any language that has the ability to call COM libraries. ...
    (microsoft.public.data.ado)
  • Re: internal alias
    ... I liked ksh93 for the regex matching.. ... script without calling it. ... You didn't know that kshis an object-oriented language? ... just an enumeration of features. ...
    (comp.unix.shell)
  • Re: Sound of j and z in Hindi films and songs
    ... "Official Policy" to remove the dots from j-dot etc, ... there was a practice of writing an anuswar on certain words like ... the dictionaries and the script followed later. ... I also notice that the more vibrant a language more tolerant it is. ...
    (rec.music.indian.misc)
  • Re: How do i convert text string to hex values as string?
    ... addition is faster than concatenation" is a valid justification for the use ... because QuickBASIC had by far the best string handling ... middle-level language, such as C. ... More in point, however, VBS is a utility script designed for relatively ...
    (microsoft.public.scripting.vbscript)