是否可以设置一个基本的HTML页面以在加载时重定向到另一个页面?
当前回答
JavaScript
<script type="text/javascript">
window.location.href = "http://example.com";
</script>
Meta标记
<meta http-equiv="refresh" content="0;url=http://example.com">
其他回答
这是前面所有答案的总结,加上通过.htaccess使用HTTP Refresh Header的附加解决方案
HTTP刷新标头首先,您可以使用.htaccess设置这样的刷新标头标题集刷新“3”这相当于在PHP中使用header()函数标头(“刷新:3;”);请注意,并非每个浏览器都支持此解决方案。JavaScript使用备用URL:<脚本>setTimeout(function){location.href=“http://example.com/alternate_url.html"} , 3000);</script>没有备用URL:<脚本>setTimeout(“location.reload(true);”,timeoutPeriod);</script>使用窗口对象:<脚本>window.location.reload(true);</script>刷新标识当不需要依赖JavaScript和重定向头时,可以使用元刷新使用备用URL:<meta http equiv=“刷新”content=“3;url=http://example.com/alternate_url.html">没有备用URL:<meta http equiv=“刷新”content=“3”>使用<noscript>:<noscript><meta http equiv=“refresh”content=“3”/></noscript>
可选
根据Billy Moon的建议,如果出现问题,您可以提供刷新链接:
如果未自动重定向:<a href='http://example.com/alternat_url.html'>单击此处</a>
资源
维基百科元刷新META REFRESH的性能影响使用jQuery刷新(重新加载)一次页面?
只是为了更好地衡量:
<?php
header("Location: http://example.com", true, 302);
exit;
?>
确保脚本上方没有echo,否则将忽略它。http://php.net/manual/en/function.header.php
我在使用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>
这似乎在所有情况下对我都有效。
这是一个重定向解决方案,包含了我想要的一切,但我找不到一个干净的片段来剪切和粘贴。
这段代码有很多优点:
允许您捕获并保留用户在其URL上的任何查询字符串参数使链接唯一,以避免不必要的缓存允许您通知用户新旧站点名称显示可设置的倒计时可以作为保留参数用于深度链接重定向
如何使用:
如果您迁移了整个站点,则在旧服务器上停止原始站点,并在根文件夹中创建另一个将此文件作为默认index.html文件的站点。编辑站点设置,以便将任何404错误重定向到此index.html页面。这将捕获任何通过链接访问旧站点的人,链接到子级页面等。
现在转到打开的脚本标记,编辑oldsite和newSite网址,并根据需要更改秒值。
保存并启动您的网站。工作完成了——该喝杯咖啡了。
<!DOCTYPE html><html><head><META HTTP-EQUIV=“cachecontrol”CONTENT=“NO-CACHE”><META HTTP-EQUIV=“PRAGMA”CONTENT=“NO-CACHE”><META HTTP-EQUIV=“到期”内容=“2002年7月22日星期一11:12:01 GMT”><style>正文{margin:200px;font:12pt helvetica;}</style></head><body></body><script type=“text/javascript”>//编辑这些以满足您的需求。var oldsite='http://theoldsitename.com'var newSite=“https://thenewsitename.com";var秒=20;//倒计时延迟。var路径=位置.pathname;var srch=位置搜索;var uniq=数学地板((Math.random()*10000)+1);var newPath=新站点+路径+(srch==“”?“?”+uniq:srch+“&”+uniq);document.write(作为主机改进的一部分,系统已从'+oldsite+'迁移到</p>');document.write('<p><a href=“'+newPath+'”>'+newSite+'</a></p>');document.write(“<p>请注意新的网站地址。</p>”);document.write(“<p>如果未自动重定向,请单击上面的链接继续。</p>”);document.write('<p id=“dvCountDown”>您将在<span id=“lblCount”></span>秒后被重定向。</p>');函数DelayRedirect(){var dvCountDown=document.getElementById(“dvCountDown”);var lblCount=document.getElementById(“lblCount”);dvCountDown.style.display=“块”;lblCount.innerHTML=秒;setInterval(函数(){秒--;lblCount.innerHTML=秒;如果(秒==0){dvCountDown.style.display=“无”;window.location=newPath;}}, 1000);}延迟重定向()</script></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 -->
推荐文章
- 防止滚动条增加到Chrome页面的宽度
- 使用图像而不是单选按钮
- 我可以嵌套一个<按钮>元素内< >使用HTML5?
- 设置TextView文本从html格式的字符串资源在XML
- 为什么我的球(物体)没有缩小/消失?
- Twitter Bootstrap 3 Sticky Footer
- CSS在特定的“内联块”项之前/之后换行
- 很好的初学者教程socket.io?
- 点击下载Href图片链接
- HtmlSpecialChars在JavaScript中等价于什么?
- 我如何删除背景图像在css?
- 如何克服HTML表单嵌套的限制?
- 如何使Bootstrap 4卡在卡列相同的高度?
- 使用域集图例引导
- 音频停止功能