Click to See Complete Forum and Search --> : Simple Conversion


GavinPearce
07-08-2003, 11:00 AM
Just a simple script, need to convert MB to KB to Bytes.

I'd like to have three text boxes one below the other, fill in a value in any of them, click go and it converts it to the other two, so the text boxes now display the values.

For example say I put in 100 in the kb box, it would then put the right one in the box above which would be MB and the right conversion in teh box below (which is bytes)

I nice touch would be when I next click on the textbox to enter the next value they automatically clear to allow the enxt input straight away.

Below is a script I found that may help.


<HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function convert() {
if(document.KBtoMB.KB.value) {
document.KBtoMB.MB.value = eval(document.KBtoMB.KB.value / 1024);
}
else {
if(document.KBtoMB.MB.value) {
document.KBtoMB.KB.value = eval(document.KBtoMB.MB.value * 1024);
}
}
}
// End -->
</script>

</HEAD>

<BODY>

<center>
<form name="KBtoMB">
<input type=text name="KB" size="16">KB
<input type=button name="KBtoMBgo" value=" < = > " onClick="convert()">
<input type=text name="MB" size="16">MB
<input type=reset value=" Clear ">
</form>
</center>


Thank you loads!!!!!!!!!!

Charles
07-08-2003, 12:32 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>This Bytes</title>
<style type="text/css">
<!--
label {display:block; margin:1ex}
-->
</style>
<script type="text/javascript">
<!--
function Bytes (b) {this.bytes = Number (b)}
Bytes.prototype.toKBytes = function () {return this.bytes / 1024}
Bytes.prototype.toMBytes = function () {return this.bytes / (1024 * 1024)}

function KBytes (k) {this.kbytes = Number (k)}
KBytes.prototype.toBytes = function () {return this.kbytes * 1024}
KBytes.prototype.toMBytes = function () {return this.kbytes / 1024}

function MBytes (m) {this.mbytes = Number (m)}
MBytes.prototype.toBytes = function () {return this.mbytes * 1024 * 1024}
MBytes.prototype.toKBytes = function () {return this.mbytes * 1024}
// -->
</script>
<form action="">
<div>
<label><input type="text" name="bytes" onchange="this.b = new Bytes(this.value); this.form.kBytes.value = this.b.toKBytes(); this.form.mBytes.value = this.b.toMBytes()">Bytes</label>
<label><input type="text" name="kBytes" onchange="this.k = new KBytes(this.value); this.form.bytes.value = this.k.toBytes(); this.form.mBytes.value = this.k.toMBytes()">K Bytes</label>
<label><input type="text" name="mBytes" onchange="m = new MBytes(this.value); this.form.kBytes.value = m.toKBytes(); this.form.bytes.value = m.toBytes()">M Bytes</label>
</div>
</form>

GavinPearce
07-09-2003, 12:32 PM
Thank you loads!!

If you give me a link, I'll post it on my new site.