I'm trying to create a button that, when clicked, calls a Javascript function that will calculate the Wifi speed and then return that value in a text box below. I've been following this example code:
Current Location: <BR>
<button onclick="getLocation()">Locate</button>
<p id="demo">
Latitude: <input type="text" id="lat">
Longitude: <input type="text" id="lon">
</p>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
document.getElementById("lat").value = position.coords.latitude;
document.getElementById("lon").value = position.coords.longitude;
}
</script>
And created this code for the wifi speed collection:
Current Wifi Speed: <BR>
<button onclick="calculateWifiSpeed()">Calculate</button>
<p id="demo">
Wifi Speed: <input type="text" id="wifiSpeed">
</p>
<script>
calculateWifiSpeed(){
var imageAddr = "http://www.kenrockwell.com/contax/images/g2/examples/31120037-5mb.jpg";
var downloadSize = 4995374; //bytes
window.onload = function() {
var oProgress = document.getElementById("progress");
oProgress.innerHTML = "Loading the image, please wait...";
window.setTimeout(MeasureConnectionSpeed, 1);
};
function MeasureConnectionSpeed() {
var oProgress = document.getElementById("progress");
var startTime, endTime;
var download = new Image();
download.onload = function () {
endTime = (new Date()).getTime();
showResults();
}
download.onerror = function (err, msg) {
oProgress.innerHTML = "Invalid image, or error downloading";
}
startTime = (new Date()).getTime();
var cacheBuster = "?nnn=" + startTime;
download.src = imageAddr + cacheBuster;
function showResults() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = downloadSize * 8;
var speedMbps = (speedKbps / 1024).toFixed(2);
document.getElementById("wifiSpeed").value = speedMbps;
oProgress.innerHTML = "Your connection speed is: <br />" + speedMbps + " Mbps<br />";
}
}
}
However, this button doesn't work and doesn't return anything.
document.getElementById("buttonId").onclick = yourFunction