javascript 在 window.location.replace 中使用参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30938882/
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
use parameters in window.location.replace
提问by mOna
I have the following script which works fine:
我有以下脚本可以正常工作:
if(error1 == 1){
window.alert('You have already signed up!');
window.location.replace('index.php');
}
But when I want to use the following URL instead of index.php, it does not substitute the parameters:
但是当我想使用以下 URL 而不是 index.php 时,它不会替换参数:
if(error1 == 1){
window.alert('You have already signed up!');
window.location.replace('http://polimovie.deib.polimi.it/Version3/index.php?campaign=".$Campaign_id."&worker=".$Worker_id."');
}
This code, redirects me to http://polimovie.deib.polimi.it/Version3/index.php?campaign=.$Campaign_id.&worker=.$Worker_id.
while I need the parameters be replaced with their actual numbers.
这段代码将我重定向到,http://polimovie.deib.polimi.it/Version3/index.php?campaign=.$Campaign_id.&worker=.$Worker_id.
而我需要将参数替换为它们的实际数字。
I know I can use php header, I also used it within my php codes like this and with both it workerd fine:
我知道我可以使用 php 标头,我也在像这样的 php 代码中使用它,并且两者都可以正常工作:
echo "<script>alert('PLEASE SIGN UP FIRST!');window.location = 'http://polimovie.deib.polimi.it/Version3/index.php?campaign=".$Campaign_id."&worker=".$Worker_id."';</script>";
but I would like to know how I can use parameters with window.location.replace.
但我想知道如何在 window.location.replace 中使用参数。
Thanks,
谢谢,
回答by AnkiiG
Try as below :
尝试如下:
if(error1 == 1){
window.alert('You have already signed up!');
window.location.replace('http://polimovie.deib.polimi.it/Version3/index.php?campaign=<?php echo $Campaign_id; ?>&worker=<?php echo $Worker_id; ?>');
}
Above will work if this is a php file and not js file
如果这是一个 php 文件而不是 js 文件,上面将起作用
回答by PHP Team
@Mona: In the code given below i have used produt_id as the javascript variable.
@Mona:在下面给出的代码中,我使用 produt_id 作为 javascript 变量。
var product_id=1;
window.location.replace("/newpage/page.php?id=" + product_id);
So, if you will compare this with your code you are required to take the values of your php variable in the javascript variable and then you have to pass them according to the above method which i have given in the example.
因此,如果您将其与您的代码进行比较,您需要在 javascript 变量中获取 php 变量的值,然后您必须根据我在示例中给出的上述方法传递它们。
Hope this will make your day!
希望这会让你开心!
Cheers :) :P
干杯 :) :P