473,378 Members | 1,469 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,378 software developers and data experts.

Script Help

Help..

We cannot get this script to work..can someone take a look at it and make
suggestions?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Convert Temperature</title>
<script language="javascript">
<!-- hide from incompatible browsers
function convert_celcius(){
(document.fahrenheit.value - 32)*5/9;
}
function convert_fahrenheit(){
(document.celcius.value * 1.8)+32;
}
//stop hiding from incompatable browsers -->
</script>
</head>
<body><pre>
<h1>Convert Temperature</h1>
<h3>Enter the temperature in Celcius</h3>
<input type="text" name="fahrenheit"
onChange="fahrenheit.value";>
<input type="button" value="Convert to Celcius"
onClick="alert('Converted to celcius' + convert_celcius[])";><br />
<h3>Enter the temperature in Fahrenheit</h3>
<input type="text" name="celcius"
onChange="celcius.value";>
<input type="button" value="Convert to Fahrenheit"
onClick="alert('Converted to Fahrenheit ' + convert_fahrenheit[]";>
</pre></body>
</html>
Jul 23 '05 #1
6 1589
"Brian" <jo*@joe.com> wrote in message news:2t*************@uni-berlin.de...
Help..

We cannot get this script to work..can someone take a look at it and make
suggestions?


[snip]

<html>
<head>
<title>Convert Temperature</title>
<script type="text/javascript">
function convert_celcius() {
var what = document.getElementById("fahrenheit").value;
var temp = (what - 32) * 5 / 9;
document.getElementById("celcius").value = temp;
return what + " C = " + temp + " F";
}
function convert_fahrenheit() {
var what = document.getElementById("celcius").value;
var temp = (what * 1.8) + 32;
document.getElementById("fahrenheit").value = temp;
return what + " F = " + temp + " C";
}
</script>
</head>
<body>
<pre>
<h1>Convert Temperature</h1>
<h3>Enter the temperature in Celcius</h3>
<input type="text" name="celcius">
<input type="button" value="Convert to Fahrenheit"
onClick="alert(convert_fahrenheit())">
<br />
<h3>Enter the temperature in Fahrenheit</h3>
<input type="text" name="fahrenheit">
<input type="button" value="Convert to Celcius"
onClick="alert(convert_celcius())">
</pre>
</body>
</html>
Jul 23 '05 #2
On Tue, 19 Oct 2004 14:30:12 -0500, Brian <jo*@joe.com> wrote:
Help..

We cannot get this script to work..
In future, please explain what you mean. We can't read minds: you need to
state what *should* occur, what *does* occur, and possibly why that's a
problem.

As things stand, the reasons are clear, here.
can someone take a look at it and make suggestions?
Certainly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
You should include the URL.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

Omitting it can fail to switch some browsers into "strict rendering" mode.

[snip]
<script language="javascript">
The language attribute has been deprecated for several years now. Instead,
use the (required) type attribute.

<script type="text/javascript">
<!-- hide from incompatible browsers
Such browsers aren't in use anymore. If I remember correctly, even
Netscape 2 understood what a SCRIPT element is, and Netscape 4 (and its
generation) are considered the oldest browsers to be considered (and even
that's debatable).
function convert_celcius(){
(document.fahrenheit.value - 32)*5/9;
It's unreasonable to assume that this will obtain a reference to the
control, fahrenheit. You also fail to do anything with the result.

Read the FAQ (<URL:http://jibbering.com/faq/>) and its notes
(<URL:http://www.jibbering.com/faq/faq_notes/faq_notes.html>). It contains
information on how to reference elements. You'll either need to include a
FORM, or reference the element with its id.
}
function convert_fahrenheit(){
(document.celcius.value * 1.8)+32;
The same applies here.
}
//stop hiding from incompatable browsers -->
This should be deleted along with the other SGML comment delimiter.
</script>
</head>
<body><pre>
Why a PRE element? I don't see any preformatted text.
<h1>Convert Temperature</h1>
<h3>Enter the temperature in Celcius</h3>
You shouldn't skip heading levels. If you want to change the style of a
heading, use CSS.
<input type="text" name="fahrenheit"
onChange="fahrenheit.value";>
That doesn't do anything. It also suffers from the "bad referencing",
earlier.

For future reference, code inside an intrinsic event can use the this
operator to refer to the current element.
<input type="button" value="Convert to Celcius"
onClick="alert('Converted to celcius' + convert_celcius[])";>
Functions are called using parentheses, not square brackets.
<br />


You're not writing XHTML, so the slash above is invalid.

From this point on, the mistakes above are repeated.

[snip]

Here's a reworked version of your page:

<URL:http://www.mlwinter.pwp.blueyonder.co.uk/clj/brian/basic.html>

It also includes input validation (will accept integers, reals, and
scientific notation) and number formatting (currently set to display 2
decimal places - the toFixed function handles that).

You'll probably want to ignore the isReal and toFixed functions at the
moment. They aren't really readable. If you want to see the effects of
toFixed, the arguments are:

n - The number to format.
p - The number of decimal places to display. Rounding is performed
automatically. The number here must be no less than 0, and no
more than 20 (the default is zero - return an integer).
g - The grouping symbol to use every three digits (the default is
no symbol).

For example,

toFixed(1234.56, 1, ',')

will produce the string, "1,234.6".

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
On Tue, 19 Oct 2004 19:59:00 GMT, McKirahan <Ne**@McKirahan.com> wrote:

[snip]
var what = document.getElementById("fahrenheit").value;
[snip]
<input type="text" name="fahrenheit">


Mind explaining that, please?

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsf4zjcttx13kvk@atlantis...
On Tue, 19 Oct 2004 19:59:00 GMT, McKirahan <Ne**@McKirahan.com> wrote:

[snip]
var what = document.getElementById("fahrenheit").value;


[snip]
<input type="text" name="fahrenheit">


Mind explaining that, please?

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


What is your question?

I suppose you are suggesting that I use "id=";
I tested this under IE which supports "name=".

Or did you want a reference to a form field?

Would you prefer the following?

<html>
<head>
<title>Convert Temperature</title>
<script type="text/javascript">
function convert_celcius() {
var what = document.getElementById("fahrenheit").value;
var temp = (what - 32) * 5 / 9;
document.getElementById("celcius").value = temp;
return what + " F = " + temp + " C";
}
function convert_fahrenheit() {
var what = document.getElementById("celcius").value;
var temp = (what * 1.8) + 32;
document.getElementById("fahrenheit").value = temp;
return what + " C = " + temp + " F";
}
</script>
</head>
<body>
<pre>
<h1>Convert Temperature</h1>
<h3>Enter the temperature in Celcius</h3>
<input type="text" id="celcius">
<input type="button" value="Convert to Fahrenheit"
onClick="alert(convert_fahrenheit())">
<br />
<h3>Enter the temperature in Fahrenheit</h3>
<input type="text" id="fahrenheit">
<input type="button" value="Convert to Celcius"
onClick="alert(convert_celcius())">
</pre>
</body>
</html>

Note that the "return" statements were switched from before.
Jul 23 '05 #5
On Wed, 20 Oct 2004 09:53:30 GMT, McKirahan <Ne**@McKirahan.com> wrote:

[snip]
I suppose you are suggesting that I use "id=";
You have to!
I tested this under IE which supports "name=".
Incorrectly.
Or did you want a reference to a form field?
That would do just as well. Probably better, in fact.
Would you prefer the following?


Yes. :)

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
McKirahan wrote:

<--snip-->

What is your question?

I suppose you are suggesting that I use "id=";
Personally, I would use name= and the forms collection.
I tested this under IE which supports "name=".
IE supports a lot of things that isn't cross-browser. Thats why it sucks
as a test browser.
Or did you want a reference to a form field?
Thats more cross-browser and more backwards compatible.
Would you prefer the following?
No, see the notes.
<html>
<head>
<title>Convert Temperature</title>
<script type="text/javascript">
function convert_celcius() {
var what = document.getElementById("fahrenheit").value;
var what = document.forms['formName'].elements['farenheit'].value;

var temp = (what - 32) * 5 / 9;
document.getElementById("celcius").value = temp;
var what = document.forms['formName'].elements['celsius'].value = temp;
return what + " F = " + temp + " C";
}
function convert_fahrenheit() {
var what = document.getElementById("celcius").value;
var what = document.forms['formName'].elements['celcius'].value;
var temp = (what * 1.8) + 32;
document.getElementById("fahrenheit").value = temp;
var what = document.forms['formName'].elements['farenheit'].value = temp;

return what + " C = " + temp + " F";
}
</script>
</head>
<body>
<pre>
<h1>Convert Temperature</h1>
<h3>Enter the temperature in Celcius</h3>
<form name="formName" action="">
<input type="text" id="celcius">
<input type="text" name="celcius">
<input type="button" value="Convert to Fahrenheit"
onClick="alert(convert_fahrenheit())">
<br />
<h3>Enter the temperature in Fahrenheit</h3>
<input type="text" id="fahrenheit">
<input type="text" name="fahrenheit">
<input type="button" value="Convert to Celcius"
onClick="alert(convert_celcius())">


<form>

Drop the pre's

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7

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

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
4
by: hupjack | last post by:
I finally joined the millions of cell phone users out there. I'm the 2nd phone on what is now a family share plan. (Our two cell phones use minutes from a central 400 minute peak time pool.)...
14
by: Akbar | last post by:
Hey there, Big-time curiosity issue here... Here's the test code (it's not that long)... it's to display a large number of image links with captions, ideally pulled in from an external file...
8
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
4
by: Derek | last post by:
I have the following script in a page and it gets an error in IE 6. Says something about an invalid argument but the line number doesn't help since I can't see the javascript code when viewing...
0
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs...
9
by: Harry Smith | last post by:
While reading the documentation on IsStartupScriptRegistered, there is a reference to "client startup script" as "Determines if the client startup script is registered with the Page object." What...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
3
by: David | last post by:
On Sun, May 4, 2008 at 4:43 AM, lev <levlozhkin@gmail.comwrote: Hi, I started tidying up the script a bit, but there are some parts I don't understand or look buggy. So I'm forwarding you the...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.