JavaScript - Check Internet Speed

Submitted by: 
Language: 
Visitors have accessed this post 458 times.

In this tutorial we will create a Check Internet Speed using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Javascipt - Check Internet Speed</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <div class="alert alert-info">Internet Speed Result</div>
  19. <div id="result"></div>
  20. <br />
  21. <button type="button" onclick="speedTest()" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-right"></span> Speed Test</button>
  22. </div>
  23. </div>
  24. <script src="js/script.js"></script>
  25. </body>
  26. </html>

Creating the Script

This code contains the script of the application. This code will speed test your internet connection to check whether the speed is fast enough to use. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. var imageAddr = "https://www.sourcecodester.com/sites/default/files/styles/thumbnail/public/2018-09-12_15_41_09-index.html_.png?itok=5LSj5xGH";
  2. var downloadSize = 4226; //bytes
  3.  
  4. function showMessage(msg) {
  5.  
  6. var result = document.getElementById("result");
  7. if (result) {
  8. var actualHTML = (typeof msg == "string") ? msg : msg.join("<br />");
  9. result.innerHTML = actualHTML;
  10. }
  11. }
  12.  
  13. function startSpeedDetection() {
  14. showMessage("Please wait identifying internet speed");
  15. window.setTimeout(getConnectionSpeed, 3000);
  16. };
  17.  
  18.  
  19. function speedTest(){
  20. startSpeedDetection();
  21. }
  22.  
  23.  
  24.  
  25. function getConnectionSpeed() {
  26. var startTime, endTime;
  27. var download = new Image();
  28. download.onload = function () {
  29. endTime = (new Date()).getTime();
  30. showResults();
  31. }
  32.  
  33. download.onerror = function (err, msg) {
  34. showMessage("Error: Failed to get internet connection");
  35. }
  36.  
  37. startTime = (new Date()).getTime();
  38. var cacheBuster = "?nnn=" + startTime;
  39. download.src = imageAddr + cacheBuster;
  40.  
  41. function showResults() {
  42. var duration = (endTime - startTime) / 1000;
  43. var bitsLoaded = downloadSize * 8;
  44. var speedBps = (bitsLoaded / duration).toFixed(2);
  45. var speedKbps = (speedBps / 1024).toFixed(2);
  46. var speedMbps = (speedKbps / 1024).toFixed(2);
  47. showMessage([
  48. "<h4>Your internet connection speed is:</h4>",
  49. "<label class='text-primary'>"+speedBps+"</label> bps",
  50. "<label class='text-primary'>"+speedKbps+"</label> kbps",
  51. "<label class='text-primary'>"+speedMbps+"</label> Mbps"
  52. ]);
  53. }
  54. }

There you have it we successfully created a Check Internet Speed using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.