example-multipleServers-pretty.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  6. <title>HTML5 Speedtest</title>
  7. <script type="text/javascript" src="speedtest.js"></script>
  8. <script type="text/javascript">
  9. //LIST OF TEST SERVERS. See documentation for details if needed
  10. var SPEEDTEST_SERVERS=[
  11. { //this is my demo server, remove it
  12. name:"Speedtest Demo Server 1", //user friendly name for the server
  13. server:"//mpotdemo.fdossena.com/", //URL to the server. // at the beginning will be replaced with http:// or https:// automatically
  14. dlURL:"garbage.php", //path to download test on this server (garbage.php or replacement)
  15. ulURL:"empty.php", //path to upload test on this server (empty.php or replacement)
  16. pingURL:"empty.php", //path to ping/jitter test on this server (empty.php or replacement)
  17. getIpURL:"getIP.php" //path to getIP on this server (getIP.php or replacement)
  18. },
  19. { //this is my demo server, remove it
  20. name:"Speedtest Demo Server 2",
  21. server:"//mpotdemo2.fdossena.com/",
  22. dlURL:"garbage.php",
  23. ulURL:"empty.php",
  24. pingURL:"empty.php",
  25. getIpURL:"getIP.php"
  26. }
  27. //add other servers here, comma separated
  28. ];
  29. //INITIALIZE SPEEDTEST
  30. var s=new Speedtest(); //create speedtest object
  31. s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers
  32. s.onupdate=function(data){ //callback to update data in UI
  33. I("ip").textContent=data.clientIp;
  34. I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
  35. I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
  36. I("pingText").textContent=data.pingStatus;
  37. I("jitText").textContent=data.jitterStatus;
  38. }
  39. s.onend=function(aborted){ //callback for test ended/aborted
  40. I("startStopBtn").className=""; //show start button again
  41. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  42. initUI();
  43. }
  44. }
  45. function selectServer(){ //called when the page is fully loaded
  46. I("startStopBtn").style.display="none"; //hide start/stop button during server selection
  47. s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
  48. I("startStopBtn").style.display=""; //show start/stop button again
  49. I("serverId").textContent=server.name; //show name of test server
  50. });
  51. }
  52. function startStop(){ //start/stop button pressed
  53. if(s.getState()==3){
  54. //speedtest is running, abort
  55. s.abort();
  56. }else{
  57. //test is not running, begin
  58. s.start();
  59. I("startStopBtn").className="running";
  60. }
  61. }
  62. //function to (re)initialize UI
  63. function initUI(){
  64. I("dlText").textContent="";
  65. I("ulText").textContent="";
  66. I("pingText").textContent="";
  67. I("jitText").textContent="";
  68. I("ip").textContent="";
  69. }
  70. function I(id){return document.getElementById(id);}
  71. </script>
  72. <style type="text/css">
  73. html,body{
  74. border:none; padding:0; margin:0;
  75. background:#FFFFFF;
  76. color:#202020;
  77. }
  78. body{
  79. text-align:center;
  80. font-family:"Roboto",sans-serif;
  81. }
  82. h1{
  83. color:#404040;
  84. }
  85. #startStopBtn{
  86. display:inline-block;
  87. margin:0 auto;
  88. color:#6060AA;
  89. background-color:rgba(0,0,0,0);
  90. border:0.15em solid #6060FF;
  91. border-radius:0.3em;
  92. transition:all 0.3s;
  93. box-sizing:border-box;
  94. width:8em; height:3em;
  95. line-height:2.7em;
  96. cursor:pointer;
  97. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  98. }
  99. #startStopBtn:hover{
  100. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  101. }
  102. #startStopBtn.running{
  103. background-color:#FF3030;
  104. border-color:#FF6060;
  105. color:#FFFFFF;
  106. }
  107. #startStopBtn:before{
  108. content:"Start";
  109. }
  110. #startStopBtn.running:before{
  111. content:"Abort";
  112. }
  113. #test{
  114. margin-top:2em;
  115. margin-bottom:12em;
  116. }
  117. div.testArea{
  118. display:inline-block;
  119. width:14em;
  120. height:9em;
  121. position:relative;
  122. box-sizing:border-box;
  123. }
  124. div.testName{
  125. position:absolute;
  126. top:0.1em; left:0;
  127. width:100%;
  128. font-size:1.4em;
  129. z-index:9;
  130. }
  131. div.meterText{
  132. position:absolute;
  133. bottom:1.5em; left:0;
  134. width:100%;
  135. font-size:2.5em;
  136. z-index:9;
  137. }
  138. #dlText{
  139. color:#6060AA;
  140. }
  141. #ulText{
  142. color:#309030;
  143. }
  144. #pingText,#jitText{
  145. color:#AA6060;
  146. }
  147. div.meterText:empty:before{
  148. color:#505050 !important;
  149. content:"0.00";
  150. }
  151. div.unit{
  152. position:absolute;
  153. bottom:2em; left:0;
  154. width:100%;
  155. z-index:9;
  156. }
  157. div.testGroup{
  158. display:inline-block;
  159. }
  160. @media all and (max-width:65em){
  161. body{
  162. font-size:1.5vw;
  163. }
  164. }
  165. @media all and (max-width:40em){
  166. body{
  167. font-size:0.8em;
  168. }
  169. div.testGroup{
  170. display:block;
  171. margin: 0 auto;
  172. }
  173. }
  174. </style>
  175. </head>
  176. <body>
  177. <h1>HTML5 Speedtest</h1>
  178. <div id="startStopBtn" onclick="startStop()"></div>
  179. <div id="serverId">Selecting server...</div>
  180. <div id="test">
  181. <div class="testGroup">
  182. <div class="testArea">
  183. <div class="testName">Download</div>
  184. <div id="dlText" class="meterText"></div>
  185. <div class="unit">Mbps</div>
  186. </div>
  187. <div class="testArea">
  188. <div class="testName">Upload</div>
  189. <div id="ulText" class="meterText"></div>
  190. <div class="unit">Mbps</div>
  191. </div>
  192. </div>
  193. <div class="testGroup">
  194. <div class="testArea">
  195. <div class="testName">Ping</div>
  196. <div id="pingText" class="meterText"></div>
  197. <div class="unit">ms</div>
  198. </div>
  199. <div class="testArea">
  200. <div class="testName">Jitter</div>
  201. <div id="jitText" class="meterText"></div>
  202. <div class="unit">ms</div>
  203. </div>
  204. </div>
  205. <div id="ipArea">
  206. IP Address: <span id="ip"></span>
  207. </div>
  208. </div>
  209. <a href="https://github.com/adolfintel/speedtest">Source code</a>
  210. <script type="text/javascript">
  211. initUI();
  212. selectServer();
  213. </script>
  214. </body>
  215. </html>