Can anybody check my code?

R

roohbir

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;
}
 
E

Evertjan.

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"
 
W

web.dev

roohbir said:
<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>

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).
 
R

roohbir

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

roohbir said:
<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>

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top