如何用js判断是否能连通某网页

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/fukaiit/article/details/102654324
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    body {
        background: grey;
    }
    </style>
    <script type="text/javascript">
    function connected() {
        console.log('连接成功,跳转到登录页');
        window.location = 'http://localhost:83/login.html';
    }

    function tryAgain() {//测试60s大概尝试连接90次,可限定200次后不再尝试
        console.log('连接失败,尝试再次连接'); 
        document.getElementById('detect').src = 'http://localhost:83/images/logo.png?i=' + Math.random();//因为图片缓存原因,增加随机变量
    }
    </script>
</head>

<body>
    <img id="detect" src="" onload="connected()" onerror="tryAgain()" />
</body>

</html>

猜你喜欢

转载自blog.csdn.net/fukaiit/article/details/102654324
今日推荐