Javascript 页面加载时的 HTML 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46498522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML redirect on page load
提问by Matthew
I need one of my website pages to instantly redirect to another upon loading. The refresh HTML command does not work, as it does not check whether or not a certain url is being loaded. Also javascript will work too.
我需要我的网站页面之一在加载时立即重定向到另一个页面。refresh HTML 命令不起作用,因为它不检查是否正在加载某个 url。javascript 也可以工作。
回答by DavSanchez
You can wait for a load eventwith JavaScript and use one of either:
您可以使用 JavaScript等待加载事件并使用以下任一方式:
window.onload = function() {
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
}
or
或者
window.onload = function() {
// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
}
Source: How to redirect to another webpage?
来源:如何重定向到另一个网页?
回答by Jacman
Just add a metatag in the headsection of your HTMLlike this:
只需meta在像这样的head部分添加一个标签HTML:
<html>
<head>
<meta http-equiv="refresh" content="0;url=http://redirect-to-this-page.com" />
<title></title>
</head>
<body></body>
</html>

