back to source codes page<<

Length Converter (centimeters, meters, kilometers, miles, inches, feet and yards)

Insert In <HEAD> Section Of Your Page

<script type="text/javascript">

// Length Converter script- By Bruce Zhang
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// Please keep this notice intact

var factors1 = new Array(1, 0.01, 0.00001, 0.00000621, 0.3937, 0.0328, 0.01094);
var factors2 = new Array(100, 1, 0.001, 0.000621, 39.37, 3.28, 1.094);
var factors3 = new Array(100000, 1000, 1, 0.621, 39370, 3280, 1094);
var factors4 = new Array(160934, 1609.34, 1.60934, 1, 63360, 5280,1760);
var factors5 = new Array(2.54, 0.0254, 0.0000254, 0.0000158, 1, 0.08333, 0.02778);
var factors6 = new Array(30.48, 0.3048, 0.0003048, 0.0001896, 12, 1, 0.3333);
var factors7 = new Array(91.44, 0.9144, 0.0009144, 0.0005688, 36, 3, 1);
var factors = new Array(factors1,factors2,factors3,factors4,factors5,factors6,factors7);

function convert_unit()
{
from_index = document.length_con.from_unit.selectedIndex;
to_index = document.length_con.to_unit.selectedIndex;
factor = factors[from_index][to_index];
document.getElementById("formula").innerHTML = document.length_con.from_unit.options[document.length_con.from_unit.selectedIndex].text + " = " + factor + " " + document.length_con.to_unit.options[document.length_con.to_unit.selectedIndex].text;
if(isNaN(document.length_con.from_value.value))
document.getElementById("to_value").innerHTML = "Not a valid number.";
else
document.getElementById("to_value").innerHTML = factor * document.length_con.from_value.value;
}
</script>

Insert In <BODY> Section Of Your Page

<h2>Length Converter</h2>
<p>
<b>1</b> select a length unit from the "From" dropdown list and one from the "To" dropdown list.
<br><b>2</b> Enter a numeric value, and click "To" button.
<p><form name="length_con">

<table>

<tr>
<td>From </td>

<td>
<select name=from_unit onChange="convert_unit()";>
<option> centimeters
<option> meters
<option> kilometers
<option> miles
<option> inches
<option> feet
<option> yards
</select>

</td>
<td> To </td>
<td>
<select name=to_unit onChange="convert_unit()";>
<option> centimeters
<option> meters
<option> kilometers
<option> miles
<option> inches
<option> feet
<option> yards
</select>

</td>
</tr>
<tr>
<td> </td><td colspan="3"><div id="formula">centimeters = 1 centimeters</div></td>
</tr>

<tr>
<td colspan="4"> </td>
</tr>

<tr>
<td>Enter </td>
<td><input type="text" name="from_value" value="1" size="12" maxlength="12"></td>
<td> <input type=button value="To" onClick="convert_unit()";> </td>
<td><div id="to_value">1</div></td>
</tr>

<tr>
<td><div align="center" style="font: normal 11px Arial">This free script provided by<br>
<a href="http://www.javascriptkit.com">JavaScript Kit</a></div></td></tr>

</table>

</form>