Html 一定时间后重定向网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3292038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:49:47  来源:igfitidea点击:

Redirect website after certain amount of time

htmlredirect

提问by codedude

What do I have to do to have a function on a website where it says it will redirect you to the site in 3 seconds or so?

我必须做些什么才能在网站上使用一个功能,它会在 3 秒左右的时间内将您重定向到该网站?

回答by Darin Dimitrov

<meta http-equiv="refresh" content="3;url=http://www.google.com/" />

回答by LukeH

You're probably looking for the metarefreshtag:

您可能正在寻找metarefresh标签

<html>
    <head>
        <meta http-equiv="refresh" content="3;url=http://www.somewhere.com/" />
    </head>
    <body>
        <h1>Redirecting in 3 seconds...</h1>
    </body>
</html>

Note that use of metarefreshis deprecated and frowned upon these days, but sometimes it's the only viable option (for example, if you're unable to do server-side generation of HTTP redirect headers and/or you need to support non-JavaScript clients etc).

请注意,metarefresh现在不推荐使用 并且不赞成使用,但有时它是唯一可行的选择(例如,如果您无法在服务器端生成 HTTP 重定向标头和/或您需要支持非 JavaScript 客户端等)。

回答by mbrevoort

If you want greater control you can use javascript rather than use the meta tag. This would allow you to have a visual of some kind, e.g. a countdown.

如果您想要更好的控制,您可以使用 javascript 而不是使用元标记。这将允许您具有某种视觉效果,例如倒计时。

Here is a very basic approach using setTimeout()

这是一个非常基本的方法,使用 setTimeout()

<html>
    <body>
    <p>You will be redirected in 3 seconds</p>
    <script>
        var timer = setTimeout(function() {
            window.location='http://example.com'
        }, 3000);
    </script>
</body>
</html>

回答by noamtm

Here's a complete (yet simple) example of redirecting after X seconds, while updating a counter div:

这是 X 秒后重定向的完整(但简单)示例,同时更新计数器 div:

<html>
<body>
    <div id="counter">5</div>
    <script>
        setInterval(function() {
            var div = document.querySelector("#counter");
            var count = div.textContent * 1 - 1;
            div.textContent = count;
            if (count <= 0) {
                window.location.replace("https://example.com");
            }
        }, 1000);
    </script>
</body>
</html>

The initial content of the counterdiv is the number of seconds to wait.

counterdiv的初始内容是等待的秒数。

回答by Ehsan

The simplest way is using HTML META tag like this:

最简单的方法是使用 HTML META 标签,如下所示:

<meta http-equiv="refresh" content="3;url=http://example.com/" />

Wikipedia

维基百科

回答by Muhammad Saqib

Place the following HTML redirect code between the and tags of your HTML code.

将以下 HTML 重定向代码放在 HTML 代码的 和 标记之间。

<meta HTTP-EQUIV="REFRESH" content="3; url=http://www.yourdomain.com/index.html">

<meta HTTP-EQUIV="REFRESH" content="3; url=http://www.yourdomain.com/index.html">

The above HTML redirect code will redirect your visitors to another web page instantly. The content="3; may be changed to the number of seconds you want the browser to wait before redirecting. 4, 5, 8, 10 or 15 seconds, etc.

上面的 HTML 重定向代码会立即将您的访问者重定向到另一个网页。content="3; 可以更改为您希望浏览器在重定向前等待的秒数。4、5、8、10 或 15 秒等。

回答by Sunny S.M

Use this simple javascript code to redirect page to another page using specific interval of time...

使用这个简单的 javascript 代码使用特定的时间间隔将页面重定向到另一个页面...

Please add this code into your web site page, which is you want to redirect :

请将此代码添加到您要重定向的网站页面中:

<script type="text/javascript">
(function(){
   setTimeout(function(){
     window.location="http://brightwaay.com/";
   },3000); /* 1000 = 1 second*/
})();
</script>