简体   繁体   English

JavaScript 重定向

[英]JavaScript Redirection

Here is the code I have,这是我的代码,

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
    setTimeout("top.location.href = url",1000);
</script>

$name is a PHP variable which has the name of which directory I'm trying to redirect to. $name是一个PHP变量,它具有我要重定向到的目录的名称。

Needless to say, it isn't working.不用说,它不起作用。 What's wrong with it?它出什么问题了? I don't know a lot about javascript so I probably did something stupid我对javascript不太了解,所以我可能做了一些愚蠢的事情

Thanks谢谢

If your mixing PHP with JavaScript, it's always advisable to check the output being sent to the browser: right click on your website and click view source!如果您将 PHP 与 JavaScript 混合使用,建议检查正在发送到浏览器的 output:右键单击您的网站并单击查看源代码!

JavaScript doesn't care whether the content being sent to it is static HTML, from a Database or generated by PHP. JavaScript 不关心发送给它的内容是 static HTML,来自数据库还是由 Z2FEC13ZCC.3222 生成If its in the output, it'll parse it.如果它在 output 中,它会解析它。

If you'd have done that, you'd notice that your not echo 'ing the $name variable.如果你已经这样做了,你会注意到你没有echo 'ing $name变量。

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/"+<? echo $name ?>+"/output.txt";
    setTimeout("top.location.href = url",1000);
</script>

But that'd give you但这会给你

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/"+ foo +"/output.txt";
    setTimeout("top.location.href = url",1000);
</script>

Which isn't valid JavaScript, as foo is now a JS variable, not a string.这不是有效的 JavaScript,因为foo现在是 JS 变量,而不是字符串。

So you should have:所以你应该有:

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
    setTimeout("top.location.href = url",1000);
</script>

Furthermore, passing a string to setTimeout (or setInterval ) is not recommend;此外,不建议将字符串传递给setTimeout (或setInterval ); for the same reasons against using eval() , so you should end up with something like this instead:出于同样的原因反对使用eval() ,所以你应该最终得到这样的东西:

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/<? echo $name ?>/output.txt";
    setTimeout(function () {
        top.location.href = url
    },1000);
</script>

Pass a function, not a string, to setTimeout .将 function 而非字符串传递给setTimeout

var url = "http://localhost:8888/uploads/<?= $name ?>/output.txt";
setTimeout(function ()
{
    top.location.href = url;
}, 1000);

I am assuming that the $name is properly filled in by PHP.我假设$name由 PHP 正确填写。

Try this:尝试这个:

setTimeout(function() { top.location.href = url; }, 1000);

Try like this:试试这样:

var url = "http://localhost:8888/uploads/"+<? $name ?>+"/output.txt";
setTimeout(function() {
    top.location.href = url;
}, 1000);
 <?php echo "var url = 'http://localhost:8888/uploads/$name/output.txt'";  ?>

Remove pluses and braces - just put variable in url.删除加号和大括号 - 只需将变量放入 url。 Also use Firebug to troubleshoot your JS code: http://getfirebug.com/还可以使用 Firebug 对您的 JS 代码进行故障排除: http://getfirebug.com/

Use 'echo' or the slightly unreadable php short code <?= to echo out a variable.使用 'echo' 或稍微不可读的 php 短代码<?=来回显一个变量。

<script language="JavaScript">
    var url = "http://localhost:8888/uploads/<?php echo $name; ?>/output.txt";
    setTimeout(function(){
        top.location = url;
    }, 1000);
</script>

I know I've created another closure, but feel it's more readable.我知道我创建了另一个闭包,但觉得它更具可读性。

It should be:它应该是:

window.location.href = url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM