User:Whiteknight/convert.js
From Wikibooks, open books for an open world
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
- Konqueror: simply click the Reload button, or press F5;
- Opera users may need to completely clear their cache in Tools→Preferences.
| Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate reading room. The code will be executed when previewing this page under some skins, including Monobook. |
|
|
Documentation for this script can be added at User:Whiteknight/convert. |
//<pre> /* 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('<script type="text/javascript" src="' + 'http://en.wikibooks.org/w/index.php?title=User:Whiteknight/convert.js' + '&action=raw&ctype=text/javascript&dontcountme=s"></script>'); */ 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"; } } } } addOnloadHook(ConvertUnitsLink); //</pre>