473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Can anybody check my code?

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

Oct 19 '06 #1
3 1098
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 <scriptis 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)
Oct 19 '06 #2

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;
}
Oct 19 '06 #3
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" <web.dev...@gmail.comwrote:
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;
}
Oct 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Macsicarr | last post by:
Hi All I know brinkster has been round for a while, but I always classed it as sort of free host site like geocities and the like. I've just had an email from them and from an ASP developer...
8
by: RikardN | last post by:
Hi, I am looking for any information regarding BerkeleyDB XML for .NET or if anybody have information regarding an alternativ (free) to BerkeleyDB XML for storing XML and using XQuery. (Yes...
5
by: PeteCresswell | last post by:
In another thread, somebody ventured the idea that this was possible under A2k's ADP but they did not sound sure of it. Anybody done it? My agenda is to use DAO for work tables bound to forms.
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
0
by: ÀÏÓà | last post by:
i just use the following simple code for test, when the code line run to flush()/close(),the simple web process will often down ! i check the web server event log ,the "sc-status sc-substatus...
4
by: rama | last post by:
Hi, I am a bit troubled with the row-level triggers which PostgreSQL uses when using update table cpmmand. For instance, if the primary key column has values 1,2,3,... and i want to update the...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: Sergey Kashyrin | last post by:
Hi, I'm accessing DB2 v7 on z/OS from Java running on NT (Database server = DB2 OS/390 7.1.1) Tried both JDBC type 4 and type 2. I'm getting regularly SQL0407N errors like this: ...
6
by: mike | last post by:
hi. trying to set up a new site with some interactivity between the pages. need a coder to set up the initial user database where visitors create their personal accounts. i'm a green...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.