在加载页面时,是否可以设置一个基本的HTML页面以重定向到另一个页面?
请将以下代码放置在HTML代码的<HEAD>和</HEAD>标签之间:
<meta HTTP-EQUIV="REFRESH" content="0; url=http://example.com/index.html">
以上的HTML重定向代码将立即将您的访问者重定向到另一个网页。 content="0;
可以更改为您希望浏览器在重定向之前等待的秒数。
我使用一个脚本,将用户从index.html重定向到相对路径的登录页面
<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>
将下面的代码放置在 <head>
区块中:
<meta http-equiv="refresh" content="0; url=http://address/">
在使用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>
这在我的所有情况下似乎都有效。
HTTP状态码301或302可以自动重定向。
对于PHP:
<?php
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.redirect-url.com");
?>
对于所有类型的页面,最简单的方法就是在头部添加一个meta
标签:
<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>
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
只是为了安全起见:
<?php
header("Location: http://example.com", true, 302);
exit;
?>
<body onload="window.location = 'http://example.com/'">
replace
方法:<script>
window.location.replace("http://example.com");
</script>