是否可以设置一个基本的HTML页面以在加载时重定向到另一个页面?
当前回答
只需使用body标签的onload事件:
<body onload="window.location = 'http://example.com/'">
其他回答
JavaScript
<script type="text/javascript">
window.location.href = "http://example.com";
</script>
Meta标记
<meta http-equiv="refresh" content="0;url=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>
您可以通过HTTP状态代码301或302自动重定向。
对于PHP:
<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.redirect-url.com");
?>
尝试使用:
<meta http-equiv="refresh" content="0; url=http://example.com/" />
注意:将其放在<head>部分。
此外,对于较旧的浏览器,如果您添加了一个快速链接,以防无法正确刷新:
<p><a href=“http://example.com/“>重定向</a></p>
将显示为
重新使用
这仍然允许您通过额外的点击到达您要去的地方。
我在使用jQuery Mobile应用程序时发现了一个问题,在某些情况下,我的Meta头标记无法正确实现重定向(jQuery Mobile不会自动读取每个页面的头,因此在那里放置JavaScript也无效,除非将其包装得更复杂)。我发现在这种情况下最简单的解决方案是将JavaScript重定向直接放入文档正文,如下所示:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=myURL" />
</head>
<body>
<p>You are not logged in!</p>
<script language="javascript">
window.location = "myURL";
</script>
</body>
</html>
这似乎在所有情况下对我都有效。