How to Check Internet Speed Test using Javascript?

Detecting internet speed using Javascript, How to detect internet speed in JavaScript, How to Detect Connection Speed With JavaScript, Detecting internet speed in Javascript, Check internet speed test in Javascript

Hi guys today i will tell you through this tutorial that you can check the internet speed of any system via javascript.

This tutorial will tell you step by step. How do I create a JavaScript page that will detect the user’s internet speed and show it on the page? So let’s go.

You can easily test the speed of any internet by javascript code as I will explain to you below the script’s code.

Firstly, by copying the code given below in the script, you will add it to your html page and get it done by id, let us test this code that has been created.

detecting-internet-speed-using-javascript.html

<script>
var imageAddr = "http://www.expertsphp.com/images/checkspeed.jpg";
var downloadSize = 4995374; //bytes
function ShowProgressMessage(msg) {
if (console) {
if (typeof msg == "string") {
console.log(msg);
} else {
for (var i = 0; i < msg.length; i++) {
console.log(msg[i]);
}
}
}
var oProgress = document.getElementById("progress");
if (oProgress) {
var actualHTML = (typeof msg == "string") ? msg : msg.join("<br />");
oProgress.innerHTML = actualHTML;
}
}
function InitiateSpeedDetection() {
ShowProgressMessage("Loading the image, please wait...");
window.setTimeout(MeasureConnectionSpeed, 1);
};
if (window.addEventListener) {
window.addEventListener('load', InitiateSpeedDetection, false);
} else if (window.attachEvent) {
window.attachEvent('onload', InitiateSpeedDetection);
}
function MeasureConnectionSpeed() {
var startTime, endTime;
var download = new Image();
download.onload = function () {
endTime = (new Date()).getTime();
showResults();
}
download.onerror = function (err, msg) {
ShowProgressMessage("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 speedBps = (bitsLoaded / duration).toFixed(2);
var speedKbps = (speedBps / 1024).toFixed(2);
var speedMbps = (speedKbps / 1024).toFixed(2);
ShowProgressMessage([
"Your connection speed is:",
speedBps + " bps",
speedKbps + " kbps",
speedMbps + " Mbps"
]);
}
}
</script>
<h1 id="progress">JavaScript Speed Test</h1>