»
EnglishFrenchVietnamese

Print - Distance Unit Conversion Calculator - JavaScriptBank.com

Full version: jsB@nk » Calculation » Equivalent » Distance Unit Conversion Calculator
URL: https://www.javascriptbank.com/distance-unit-conversion-calculator.html

Distance Unit Conversion Calculator © JavaScriptBank.comA distance converter.

Full version: jsB@nk » Calculation » Equivalent » Distance Unit Conversion Calculator
URL: https://www.javascriptbank.com/distance-unit-conversion-calculator.html



JavaScript
<script language="JavaScript"><!-- Generic Unit Conversion Program// Author    : Jonathan Weesner ([email protected])  21 Nov 95// Copyright : You want it? Take it! ... but leave the Author line intact please!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.elements[1].value = 1;    convertform(form);    return true;}function clearform(form) {    for (var i = 1; i <= form.count; i++) form.elements[i].value = "";    return true;}<!-- done hiding from old browsers --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<form><table border="4"><tbody><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></tbody></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 typesdocument.forms[0].rsize = 7;  // Rounding size, use same as SIZEdocument.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.0005399568034557; // m to nm.<!-- done hiding from old browsers --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->