是否可以设置一个基本的HTML页面以在加载时重定向到另一个页面?


当前回答

将以下代码放入<head>部分:

<meta http-equiv="refresh" content="0; url=http://address/">

其他回答

将以下代码放入<head>部分:

<meta http-equiv="refresh" content="0; url=http://address/">

一旦页面加载,init函数就会启动并重定向页面:

<!DOCTYPE html>
<html>
    <head>
        <title>Example</title>
        <script>
            function init()
            {
               window.location.href = "www.wherever.com";
            }
        </script>
    </head>

    <body onload="init()">
    </body>
</html>

我使用一个脚本将用户从index.html重定向到登录页面的相对url

<html>
  <head>
    <title>index.html</title>
  </head>
  <body onload="document.getElementById('lnkhome').click();">
    <a href="/Pages/Login.aspx" id="lnkhome">Go to Login Page</a>
  </body>
</html>

如果您希望遵循现代web标准,则应避免纯HTML元重定向。如果无法创建服务器端代码,则应选择JavaScript重定向。

要支持禁用JavaScript的浏览器,请向noscript元素添加HTML元重定向行。noscript嵌套的元重定向与规范标记相结合,也有助于搜索引擎排名。

如果希望避免重定向循环,应使用location.replace()JavaScript函数。

正确的客户端URL重定向代码如下所示(带有Internet Explorer 8和更低版本的修复程序,且无延迟):

<!-- Pleace this snippet right after opening the head tag to make it work properly -->

<!-- This code is licensed under GNU GPL v3 -->
<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://stackoverflow.com/"/>
<noscript>
    <meta http-equiv="refresh" content="0; URL=https://stackoverflow.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://stackoverflow.com/";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
    {
        document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->

适用于所有类型页面的简单方法是在头部添加一个元标记:

<html>
    <head>
        ...
        <meta HTTP-EQUIV="REFRESH" content="seconds; url=your.full.url/path/filename">
        ...
    </head>
    <body>
        Don't put much content, just some text and an anchor.
        Actually, you will be redirected in N seconds (as specified in content attribute).
        That's all.
        ...
    </body>
</html>