利用iframe测试进入网站的速度

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算页面加载时间</title>
    <script src="js/jquery-2.2.4.min.js" type="text/javascript"></script>
</head>
<body>
<div style="margin:10px">
    <div>
         <input type="text" class="in-url" style="width:450px;height:30px" value="http://www.8516a.com" >
        <label class="lbStatus"></label>
    </div>
    <div>
        <input type="text" class="in-url" style="width:450px;height:30px" value="http://www.8516b.com" >
        <label class="lbStatus"></label>
    </div>
    <div>
        <input type="text" class="in-url" style="width:450px;height:30px" value="http://www.8516c.com" >
        <label class="lbStatus"></label>
    </div>
    <div>
        <input type="text" class="in-url" style="width:450px;height:30px" value="http://www.8516d.com" >
        <label class="lbStatus"></label>
    </div>
    <button id="go">测速</button>
</div>
<div style="margin:10px; width:100%; height:450px; min-height:450px">
    <iframe class="fm" width="100%" height="900px" src="http://www.8516a.com" style="display: none"></iframe>
    <iframe class="fm" width="100%" height="900px" src="http://www.8516b.com" style="display: none"></iframe>
    <iframe class="fm" width="100%" height="900px" src="http://www.8516c.com" style="display: none"></iframe>
    <iframe class="fm" width="100%" height="900px" src="http://www.8516d.com" style="display: none"></iframe>
</div>
<script>
    var t1 = new Date().getTime(),t2;
    $(function(){
        $('#go').on('click',function(){
            t1 = new Date().getTime();
            var inputUrlS = $('.in-url').val();
            for(var i=0;i<inputUrlS.length;i++){
                $($('.lbStatus')[$(this).index()]).text('加载中...');
                $($('.fm')[i]).attr('src',inputUrlS[i]);
            }
        });
        $('.fm').on('load',function(){
            t2 = new Date().getTime();
            console.info(t2-t1);
            console.info($(this).index())
            $($('.lbStatus')[$(this).index()]).text('加载完毕,耗时:'+(t2-t1)+'毫秒');
        });
    });
</script>
</body>

</html>

附上源码  这里精髓就在于iframe的src属性 然后利用jq的load 去计算2个时间的时间差  从而生成所要的速度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值