Converter in From

[Total: 0    Average: 0/5]

This script is a generic unit conversion program.

Body Code:

<script LANGUAGE="JavaScript">
<!-- Generic Unit Conversion Program
function convertform(form){
    var firstvalue = 0;
    for (var i = 1; i <= form.count; i++) {
       // Find first non-blank entry
       if (form.elements[i].value != null && form.elements[i].value.length != 0) {
	  if (i == 1 && form.elements[2].value != "") return false;
	  firstvalue = form.elements[i].value / form.elements[i].factor;
	  break;
       }
    }
    if (firstvalue == 0) {
       clearform(form);
       return false;
    }
    for (var i = 1; i <= form.count; i++)
       form.elements[i].value = formatvalue((firstvalue * form.elements[i].factor), form.rsize);
    return true;
}
function formatvalue(input, rsize) {
   var invalid = "**************************";
   var nines = "999999999999999999999999";
   var strin = "" + input;
   var fltin = parseFloat(strin);
   if (strin.length <= rsize) return strin;
   if (strin.indexOf("e") != -1 ||
       fltin > parseFloat(nines.substring(0,rsize)+".4"))
      return invalid.substring(0, rsize);
   var rounded = "" + (fltin + (fltin - parseFloat(strin.substring(0, rsize))));
   return rounded.substring(0, rsize);
}
function resetform(form) {
    clearform(form);
    form[1].value = 1;
    convertform(form);
    return true;
}
function clearform(form) {
    for (var i = 1; i <= form.count; i++) form[i].value = "";
    return true;
}
<!-- done hiding from old browsers -->
</script>
<form method="POST">
  <table border="4">
    <tr>
      <td COLSPAN="7" ALIGN="CENTER" VALIGN="MIDDLE"><h2>Distance Unit Conversion Calculator</h2>
      </td>
    </tr>
    <tr>
      <td ALIGN="CENTER">Meters</td>
      <td ALIGN="CENTER">Inches</td>
      <td ALIGN="CENTER">Feet</td>
      <td ALIGN="CENTER">Yards</td>
      <td ALIGN="CENTER">Miles</td>
      <td ALIGN="CENTER">Nautical<br>Miles</td>
      <td><input TYPE="button" VALUE="Calibrate" onclick="resetform(this.form)"></td>
    </tr>
    <tr>
      <td><input TYPE="TEXT" NAME="val1" SIZE="7" onfocus="clearform(this.form)"></td>
      <td><input TYPE="TEXT" NAME="val2" SIZE="7" onfocus="clearform(this.form)"></td>
      <td><input TYPE="TEXT" NAME="val3" SIZE="7" onfocus="clearform(this.form)"></td>
      <td><input TYPE="TEXT" NAME="val4" SIZE="7" onfocus="clearform(this.form)"></td>
      <td><input TYPE="TEXT" NAME="val5" SIZE="7" onfocus="clearform(this.form)"></td>
      <td><input TYPE="TEXT" NAME="val6" SIZE="7" onfocus="clearform(this.form)"></td>
      <td><input TYPE="button" VALUE="Calculate" onclick="convertform(this.form)"></td>
    </tr>
  </table>
</form>
<script LANGUAGE="JavaScript">
<!-- Set conversion factors for each item in form. All
// factors must convert the first item to the current item.
// Be sure to use the correct form index. The first form is
// always index "0" and remaining forms are numbered in the
// order they appear in the document.
document.forms[0].count = 6;  // number of unit types
document.forms[0].rsize = 7;  // Rounding size, use same as SIZE
document.forms[0].val1.factor = 1;            // m to m.
document.forms[0].val2.factor = 39.37007874;  // m to in.
document.forms[0].val3.factor = 3.280839895;  // m to ft.
document.forms[0].val4.factor = 1.093613298;  // m to yards.
document.forms[0].val5.factor = 0.00062137119; // m to mi.
document.forms[0].val6.factor = 0.000547045; // m to nm.
<!-- done hiding from old browsers -->
</script>
<hr align="center">
<form method="POST">
  <table border="4">
    <tr>
      <td COLSPAN="7" ALIGN="CENTER" VALIGN="MIDDLE"><h2>Weight Unit Conversion Calculator</h2>
      </td>
    </tr>
    <tr>
      <td ALIGN="CENTER">Kilograms</td>
      <td ALIGN="CENTER">Ounces</td>
      <td ALIGN="CENTER">Pounds</td>
      <td ALIGN="CENTER">Troy<br>Pounds</td>
      <td ALIGN="CENTER">Stones</td>
      <td ALIGN="CENTER">Short<br>Tons</td>
      <td ALIGN="CENTER">Long<br>Tons</td>
      <td><input TYPE="button" VALUE="Calibrate" onClick="resetform(this.form)"></td>
    </tr>
    <tr>
      <td><input TYPE="TEXT" NAME="val1" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val2" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val3" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val4" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val5" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val6" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val7" SIZE="6" onFocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="button" VALUE="Calculate" onclick="convertform(this.form)"></td>
    </tr>
  </table>
</form>
<script LANGUAGE="JavaScript">
<!-- Set conversion factors for each item in form.
document.forms[1].count = 7;
document.forms[1].rsize = 6;
document.forms[1].val1.factor = 1;
document.forms[1].val2.factor = 35.273944;
document.forms[1].val3.factor = 2.2046215;
document.forms[1].val4.factor = 2.6792765;
document.forms[1].val5.factor = 0.1574731232747;
document.forms[1].val6.factor = 0.00110231075;
document.forms[1].val7.factor = 0.001;
<!-- done hiding from old browsers -->
</script>
<hr align="center">
<form method="POST">
  <table border="4">
    <tr>
      <td COLSPAN="6" ALIGN="CENTER" VALIGN="MIDDLE"><h2>Volume Unit Conversion Calculator</h2>
      </td>
    </tr>
    <tr>
      <td ALIGN="CENTER">Liters</td>
      <td ALIGN="CENTER">Fluid<br>Ounces</td>
      <td ALIGN="CENTER">Quarts</td>
      <td ALIGN="CENTER">Gallons</td>
      <td ALIGN="CENTER">Imperial<br>Gallons</td>
      <td><input TYPE="button" VALUE="Calibrate" onclick="resetform(this.form)"></td>
    </tr>
    <tr>
      <td><input TYPE="TEXT" NAME="val1" SIZE="6" onfocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val2" SIZE="6" onfocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val3" SIZE="6" onfocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val4" SIZE="6" onfocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="TEXT" NAME="val5" SIZE="6" onfocus="clearform(this.form)" </TD> </td>
      <td><input TYPE="button" VALUE="Calculate" onclick="convertform(this.form)"></td>
    </tr>
  </table>
</form>
<script LANGUAGE="JavaScript">
// Set conversion factors for each item in form.
document.forms[2].count = 5;
document.forms[2].rsize = 6;
document.forms[2].val1.factor = 1;
document.forms[2].val2.factor = 33.8239926;
document.forms[2].val3.factor = 1.056998;
document.forms[2].val4.factor = 0.2642499;
document.forms[2].val5.factor = 0.2200433;
</script>

Times Viewed: 9

Leave a Reply

Your email address will not be published. Required fields are marked *