Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can anybody check my code?

Reply
Thread Tools

Can anybody check my code?

 
 
roohbir
Guest
Posts: n/a
 
      10-19-2006
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;
}

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      10-19-2006
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)
 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      10-19-2006

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


 
Reply With Quote
 
roohbir
Guest
Posts: n/a
 
      10-20-2006
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" <(E-Mail Removed)> 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;
> > }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Anybody wanna check my company's website? Rich Grise HTML 4 06-21-2008 07:04 AM
Anybody wanna check my company's website? Rich Grise Javascript 6 06-21-2008 07:04 AM
Can anybody please check this code out? Lorenzo Java 4 01-18-2007 01:04 PM
Could anybody check the speed of my site please? Ben Jones Computer Support 22 08-18-2006 10:02 PM
Can't Open RAR files - PAR check ok, SFV check ok Your name Computer Support 3 05-09-2005 06:43 AM



Advertisments