如何从页面HTML重定向

时间:2015-12-02 04:20:21

标签: html

如何使用HTLM从页面重定向有一定延迟? 例如,我会进入一个网页,5秒后页面将重定向。

2 个答案:

答案 0 :(得分:3)

您可能正在寻找meta refresh代码:

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

来源:Redirect website after certain amount of time

答案 1 :(得分:1)

您可以使用HTML元标记。它将重定向到另一个页面。倒计时是在加载此页面后立即开始的:

<html>
  <head>
    <META http-equiv="refresh" content="5;URL=http://example.com">
  </head>
</html>

其中“5”代表5秒。您可以将0设置为立即重定向。

这是最好的方式,因为它不需要JavaScript,所有现代浏览器都支持它。

如果您不想在页面加载后执行此操作,但在执行某些操作后,您需要使用JavaScript window.location.href属性和setTimeout来延迟此操作:

setTimeout(function() {
    window.location.href = "http://example.com";
}, 5000); 

其中5000表示5000毫秒(即5秒)。