【问题标题】:How to redirect from page HTML如何从页面 HTML 重定向
【发布时间】:2016-03-06 05:31:15
【问题描述】:

如何在一定延迟的情况下使用 HTLM 从页面重定向? 例如,我会进入一个网页,5 秒后页面会重定向。

【问题讨论】:

    标签: html


    【解决方案1】:

    您可能正在寻找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

    【讨论】:

      【解决方案2】:

      您可以使用 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 秒)。

      【讨论】:

      • @RichyChai 哦,对不起,我现在会更新我的答案。你应该把它放在你的head 标签中。
      猜你喜欢
      • 2011-07-21
      • 2017-01-03
      • 2012-10-15
      • 2018-03-17
      • 2019-10-28
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多