请考虑以下代码:
<html>
<head>
<title>This is the from page</title>
</head>
<body>
<script type='text/javascript'>
function redirect(destination)
{
newWindow = window.open(destination, "_blank", "width=790,height=520");
newWindow.document.write("<meta http-equiv='refresh' content='4;url=" + destination + "'>");
newWindow.document.write("<h1>Now redirecting to destination page in 4 seconds...</h1>");
}
redirect("./to.html");
</script>
</body>
基本上我只能在4秒内看到带有现在重定向到目标页面的提示页面... 。但它永远停留在那里...... Firebug告诉我,元标记确实存在于提示页面中。
有什么想法?非常感谢提前!
答案 0 :(得分:2)
function redirect(destination)
{
setTimeout(function(){window.location = destination;},4000);
}
答案 1 :(得分:1)
<html>
<head>
<script>
// Parse the query string to get the destination URL
var params = {};
var pairs = window.location.search.substring(1).split('&');
for (var i = 0; i < pairs.length; i++) {
var components = pairs[i].split('=');
params[components[0]] = decodeURIComponent(components[1]);
}
setTimeout(function() { window.location = params.redirect; }, 4000);
</script>
</head>
<body>
<h1>Now redirecting to destination page in 4 seconds...</h1>
</body>
</html>
以下是您在主页中使用它的方式:
function redirect(destination)
{
window.open(
'redirect.html?redirect=' + encodeURIComponent(destination), "_blank",
"width=790,height=520");
}
redirect("./to.html");
答案 2 :(得分:1)
我认为您的代码无效的原因是浏览器已经处理了页面及其元数据。假设是这种情况,在事实之后添加重定向将不起作用。 (对浏览器内部有更多了解的人需要验证。)
我不确定你为什么要运行它而不只是加载页面,但是一种应该工作的方法是设置一个javascript超时来更新弹出窗口的位置。例如:
<html>
<head>
<title>This is the from page</title>
</head>
<body>
<script type="text/javascript">
function redirect(destination)
{
newWindow = window.open(destination, "_blank", "width=790,height=520");
newWindow.document.write("<h1>Now redirecting to destination page in 4 seconds...</h1>");
setTimeout(function(){ newWindow.location=destination;}, 4000);
}
redirect("./to.html");
</script>
</body>