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,Recei verName,ReceiverEmail)
{
this.DestinationAddress=DestinationAddress;
this.ShippingType=ShippingType;
this.ReceiverName=ReceiverName ;
this.ReceiverEmail=ReceiverEmail;
}