首页 > 解决方案 > 使用 JavaScript 获取 url 参数的站点加载时间

问题描述

访问domain.com?site=www.google.com时加载时出错,似乎时间是随机的。

代码:

<html>
<head><p>Website Checker</p>
<script type='text/javascript'>

           function checkhowlongittakes() {
    weburl = "<?php echo $_GET['site']; ?>";
    getLoadTime(weburl, function(time) {
        document.write('load time for ' + weburl + ' was >' + time + 'ms');
    });
}
function getLoadTime(url, callback) {
    var req = new XMLHttpRequest();
    var start = Date.now();
    req.onreadystatechange = function(e) {
        if (this.readyState == 4)
            callback(Date.now() - start);
    }
    req.open('GET', url);
    req.send();
}
        </script>
</head>



<body onload="checkhowlongittakes()">

</body>

</html>

标签: javascripthtmlpage-load-time

解决方案


推荐阅读