最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - use parameters in window.location.replace - Stack Overflow

programmeradmin3浏览0评论

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:

if(error1 == 1){
    window.alert('You have already signed up!');
    window.location.replace('.php?campaign=".$Campaign_id."&worker=".$Worker_id."');
    }

This code, redirects me to .php?campaign=.$Campaign_id.&worker=.$Worker_id. while I need the parameters be replaced with their actual numbers.

I know I can use php header, I also used it within my php codes like this and with both it workerd fine:

echo "<script>alert('PLEASE SIGN UP FIRST!');window.location = '.php?campaign=".$Campaign_id."&worker=".$Worker_id."';</script>";

but I would like to know how I can use parameters with window.location.replace.

Thanks,

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:

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.

I know I can use php header, I also used it within my php codes like this and with both it workerd fine:

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.

Thanks,

Share Improve this question asked Jun 19, 2015 at 13:02 mOnamOna 2,45910 gold badges37 silver badges63 bronze badges 1
  • is the JS inside of a PHP file which has those variables declared and filled? – Gerton Commented Jun 19, 2015 at 13:05
Add a ment  | 

2 Answers 2

Reset to default 5

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

@Mona: In the code given below i have used produt_id as the javascript variable.

var product_id=1;
window.location.replace("/newpage/page.php?id=" + product_id);

So, if you will pare 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.

Hope this will make your day!

Cheers :) :P

发布评论

评论列表(0)

  1. 暂无评论