User:Whiteknight/convert.js

//

/* Unit conversion Javascript Written by User:Whiteknight Automatically convert between different sets of equivalent units Especially Metric->American unit conversions. Use this script by writing the following into your personal javascript file:

document.write(' ');

function ConvertUnitsLink { var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0]; var link = document.createElement('a'); link.onclick = ConvertUnits; link.href = "#"; link.appendChild(document.createTextNode("Convert Units")); var li = document.createElement('li'); li.id = "custom-link"; li.appendChild(link); tb.insertBefore(li, tb.firstChild); }

function ConvertUnits { Convert("Kg", "Lbs", "conv_mass_val", "conv_mass_unit", 2.20); Convert("m", "Feet", "conv_dist_val", "conv_dist_unit", 3.28); }

function Convert(munits, iunits, valtag, unittag, mtoifact) { var spanElements = document.getElementsByTagName('span'); for (i = 0; i < spanElements.length; i++) {   if(spanElements[i].className == valtag) {     var number = parseFloat(spanElements[i].innerHTML); var result = 0.0; if(spanElements[i].value == "SAE") {       result = number / mtoifact; spanElements[i].value = "Metric"; }     else {       result = number * mtoifact; spanElements[i].value = "SAE"; }     spanElements[i].innerHTML = result + ''; }   else if(spanElements[i].className == unittag) {     if(spanElements[i].value == "SAE") {   	spanElements[i].innerHTML = munits; spanElements[i].value = "Metric"; }     else {       spanElements[i].innerHTML = iunits; spanElements[i].value = "SAE"; }   }  } }

$(ConvertUnitsLink); //