JavaScript网页制作--五秒后自动跳转页面

JavaScript网页制作–五秒后自动跳转页面

通常在浏览一些网站时,会出现网页不存在的情况,网页不存在之后,有些网站会在5秒后自动跳转到一个新的网页,比如网站的首页。可以利用定时器和location属性写自动跳转的页面

在这里插入图片描述

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>五秒之后自动跳转</title>
    <style>
        div{
            font-size: 16px;
            background-color: coral;
            height: 50px;
            width: 300px;
            text-align: center;
            line-height: 50px;
            margin: auto ;
            margin-top: 250px;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
        var div = document.querySelector('div');
        var t = 5;
        fn();
        setInterval(fn, 1000);
        function fn() {
            if (t === 0){
            location.href = 'https://blog.csdn.net/weixin_42579348';
        }    
            div.innerHTML = '页面不存在,您将在'+t+'秒后跳转至新页面';
            t--;
        }        
    </script>
</body>
</html>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值