Can anybody check my code?

Discussion in 'Javascript' started by roohbir, Oct 19, 2006.

  1. roohbir

    roohbir Guest

    I have two files: a .html file and a .js file. I have pasted both on
    this page. The functionality required of the code is that it should
    display on the clicking of submit button ShippingInfo JavaScript object
    according to the logic in the if else statement which I think is self
    explanatory. Help would be appreciated.

    The .html file:

    <html>
    <head>
    </head>

    <body>
    <script language="JavaScript"
    type="text/javascript"
    src="myOwnJavaScript.js">

    function ComputeCost()
    {
    //create an object
    obj = new ShippingInfo(sampleform.menu1.value,
    sampleform.menu2.value, sampleform.text1.value, sampleform.text2.value)

    //add a method
    obj.GetShippingCost=GetShippingCost;

    //Invoke a method.
    var Cost = obj.GetShippingCost();
    var message = document.getElementById("myOwndivElement");
    message.innerHTML = "Cost = " + Cost;
    return false;
    }
    </script>
    <form name="sampleform" onsubmit="ComputeCost();return false;">

    <p>Shipping Destination:
    <select name="menu1">
    <option value="">--Select a destination--</option>
    <option value="Boston">Boston</option>
    <option value="Seoul">Seoul</option>
    </select>


    <p>Shipping Type:
    <select name="menu2">
    <option value="">--Select a delivery method--</option>
    <option value="Express Delivery">Express Delivery</option>
    <option value="Standard Delivery">Standard Delivery</option>
    </select>


    <p>Receiver:
    <input id="text1" type="text" size="20"></input>

    <p>Receiver's Email Address:
    <input id="text2" type="text" name="email" size="20"></input>

    <p><input type="submit" value="Submit">

    <div id="myOwndivElement"></div>
    </form>
    </body>
    </html>

    The .js file:
    // This is a function that is added as a method of a JavaScript object
    function GetShippingCost()
    {
    int cost = 0;
    if(this.DestinationAddress=="Boston")
    {
    if(this.ShippingType=="Standard Delivery")
    {
    cost = 100
    }

    else(this.ShippingType=="Express Delivery")
    {
    cost = 200
    }
    }
    else if(this.DestinationAddress=="Seoul")
    {
    if(this.ShippingType=="Standard Delivery")
    {
    cost = 100
    }
    else(this.ShippingType=="Express Delivery")
    {
    cost = 200
    }
    }
    return cost;
    }

    // Define a template. Note that a template is just a function.
    // The properties are needed to be initialized with this.
    function
    ShippingInfo(DestinationAddress,ShippingType,ReceiverName,ReceiverEmail)
    {
    this.DestinationAddress=DestinationAddress;
    this.ShippingType=ShippingType;
    this.ReceiverName=ReceiverName ;
    this.ReceiverEmail=ReceiverEmail;
    }
    roohbir, Oct 19, 2006
    #1
    1. Advertising

  2. roohbir

    Evertjan. Guest

    roohbir wrote on 19 okt 2006 in comp.lang.javascript:

    > <script language="JavaScript"
    > type="text/javascript"
    > src="myOwnJavaScript.js">
    >
    > function ComputeCost()
    > ........


    You cannot do that.

    a <script> is eiter an inline script,

    <script type='text/javascript'>alert('hi');</script>

    or a linked script,

    <script type='text/javascript'
    src='yourJavascriptScript.js'></script>

    NOT !!!!! both.

    do not use: language="JavaScript"

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Oct 19, 2006
    #2
    1. Advertising

  3. roohbir

    web.dev Guest

    roohbir wrote:
    > <script language="JavaScript"
    > type="text/javascript"
    > src="myOwnJavaScript.js">


    1. The language attribute is deprecated, use the type attribute.
    2. You can either use an external javascript or embedded, but not
    both.

    <script type = "text/javascript" src = "myOwnJavaScript.js"></script>

    <script type = "text/javascript">
    //etc

    > function ComputeCost()
    > {
    > //create an object
    > obj = new ShippingInfo(sampleform.menu1.value,
    > sampleform.menu2.value, sampleform.text1.value, sampleform.text2.value)


    It's good practice to start off your variables with "var" so that there
    is ambiguity to the person maintaining the code whether the variable is
    local or global.

    A "safer" way of accessing form elements would be to use the following
    syntax:

    document.forms["formName"].elements["elementName"].value;

    > //add a method
    > obj.GetShippingCost=GetShippingCost;


    Instead of adding the method to the object, it seems it would be better
    to just have the method already a part of the object to be invoked.
    See below.

    > //Invoke a method.
    > var Cost = obj.GetShippingCost();
    > var message = document.getElementById("myOwndivElement");
    > message.innerHTML = "Cost = " + Cost;
    > return false;
    > }
    > </script>


    [snip]

    > // This is a function that is added as a method of a JavaScript object
    > function GetShippingCost()


    To add a method to the object, one can go about doing this instead:

    ShippingInfo.prototype.GetShippingCost = function()
    {
    //etc

    This way, after you create a new object, you can just invoke it like
    so:

    obj.GetShippingCost();

    > {
    > int cost = 0;


    [snip if...else]

    It could become more efficient with the use of switches instead of
    nested if..else statements. (For scalability).

    > return cost;
    > }
    web.dev, Oct 19, 2006
    #3
  4. roohbir

    roohbir Guest

    thanks a lot evertjan and web.dev. i'll try all u guys suggested and
    get back to you.
    cheers
    roohbir

    On Oct 19, 10:38 am, "web.dev" <> wrote:
    > roohbir wrote:
    > > <script language="JavaScript"
    > > type="text/javascript"
    > > src="myOwnJavaScript.js">1. The language attribute is deprecated, use the type attribute.

    > 2. You can either use an external javascript or embedded, but not
    > both.
    >
    > <script type = "text/javascript" src = "myOwnJavaScript.js"></script>
    >
    > <script type = "text/javascript">
    > //etc
    >
    > > function ComputeCost()
    > > {
    > > //create an object
    > > obj = new ShippingInfo(sampleform.menu1.value,
    > > sampleform.menu2.value, sampleform.text1.value, sampleform.text2.value)It's good practice to start off your variables with "var" so that there

    > is ambiguity to the person maintaining the code whether the variable is
    > local or global.
    >
    > A "safer" way of accessing form elements would be to use the following
    > syntax:
    >
    > document.forms["formName"].elements["elementName"].value;
    >
    > > //add a method
    > > obj.GetShippingCost=GetShippingCost;Instead of adding the method to the object, it seems it would be better

    > to just have the method already a part of the object to be invoked.
    > See below.
    >
    > > //Invoke a method.
    > > var Cost = obj.GetShippingCost();
    > > var message = document.getElementById("myOwndivElement");
    > > message.innerHTML = "Cost = " + Cost;
    > > return false;
    > > }
    > > </script>[snip]

    >
    > > // This is a function that is added as a method of a JavaScript object
    > > function GetShippingCost()To add a method to the object, one can go about doing this instead:

    >
    > ShippingInfo.prototype.GetShippingCost = function()
    > {
    > //etc
    >
    > This way, after you create a new object, you can just invoke it like
    > so:
    >
    > obj.GetShippingCost();
    >
    > > {
    > > int cost = 0;[snip if...else]

    >
    > It could become more efficient with the use of switches instead of
    > nested if..else statements. (For scalability).
    >
    > > return cost;
    > > }
    roohbir, Oct 20, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Catalin Porancea

    Can anybody help with this code?

    Catalin Porancea, Nov 6, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,336
    L. L.
    Nov 6, 2003
  2. =?iso-8859-1?q?vipul=20parmar?=

    can anybody tell me how to compile the C code with embedded python.

    =?iso-8859-1?q?vipul=20parmar?=, Mar 4, 2004, in forum: Python
    Replies:
    0
    Views:
    284
    =?iso-8859-1?q?vipul=20parmar?=
    Mar 4, 2004
  3. Lorenzo
    Replies:
    4
    Views:
    355
    Andrew Thompson
    Jan 18, 2007
  4. Rich Grise
    Replies:
    4
    Views:
    322
    John R. Carroll
    Jun 21, 2008
  5. Rich Grise

    Anybody wanna check my company's website?

    Rich Grise, Jun 20, 2008, in forum: Javascript
    Replies:
    6
    Views:
    86
    John R. Carroll
    Jun 21, 2008
Loading...

Share This Page