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


当前回答

你应该使用JavaScript。将以下代码放在头标签中:

<script type="text/javascript">
 window.location.assign("http://www.example.com")
</script>

其他回答

只需使用body标签的onload事件:

<body onload="window.location = 'http://example.com/'">

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

<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>

尝试使用:

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

注意:将其放在<head>部分。

此外,对于较旧的浏览器,如果您添加了一个快速链接,以防无法正确刷新:

<p><a href=“http://example.com/“>重定向</a></p>

将显示为

重新使用

这仍然允许您通过额外的点击到达您要去的地方。

如果您希望遵循现代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 -->

你应该使用JavaScript。将以下代码放在头标签中:

<script type="text/javascript">
 window.location.assign("http://www.example.com")
</script>