分享一个网页测速的小插件,代码如下:
<!DOCTYPE html>
<html>
<head>
<title>网速测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<SCRIPT language=JavaScript>
var start = new Date();
</SCRIPT>
<img alt="测试图片" src="https://imagecomics.com/uploads/releases/_small/Mayday_02-1.png?<?php echo time();?>" οnlοad="testspeed();" />
<script>
var arr=["拨号上网","2兆","4兆","6兆","8兆","10兆","20兆","50兆","100兆","网速超神"];
function testspeed()
{
var filesize = 274; //测试图片实际大小,单位(kb)
var end = new Date();
var speed = Math.round(filesize * 1000)/(end - start);
var scope = (speed > 0 && speed <= 50) ? 0 : (speed > 50 && speed <= 400) ? 1 : (speed >= 400 && speed < 600) ? 2 : (speed >= 600 && speed < 800) ? 3 : (speed >= 800 && speed < 1000) ? 4 : (speed >= 1000 && speed < 1300) ? 5 : (speed >= 1300 && speed < 2300) ? 6 : (speed >= 2300 && speed < 4000) ? 7 : (speed >= 4000 && speed < 7000) ? 8 : 9;
document.getElementById("speed").innerHTML = ("您的上网环境:" + arr[scope] + " ,实际下载速度:" + speed.toFixed(0) + "KB;")
}
</script>
<div id='speed'></div>
</body>
实际效果如下: