0

I am trying to dynamically add parts to a URL by passing variables to the URL dynamically such as in the code below. How can I get the code below to work so that the URL comes out looking like this:

 http://www.test.com/test/Test_1/ato.50/[atc1.100/atc2.200/atc3.RandomNumber/atc3.RandomNumber/atc3.RandomNumber/atc4.400

where RandomNumber is a randomly generated number using Math.floor(Math.random() * 10 * 10());

(The repeat of passing a number to atc3. is intentional.)

Code:

<html>
<body>
<script>
var orderID = 50;
var Red = 100;
var Blue = 200;
var Green = [Math.floor(Math.random() * 10 * 10), Math.floor(Math.random() * 10 * 10), Math.floor(Math.random() * 10 * 10)];
var Yellow = 400;

var tag = '<s'+'cript language="JavaScript" src="http://www.test.com/test/Test_1/ato.' + orderID + '/[atc1.' + Red + '/atc2.' + Blue + '/atc3.' + Green[0];

i = 1;
while (i < Green.length){
    var tag_two[i] ='/atc3.' + Green[i];
}
var tag_three ='/atc4.' + Yellow + ']"></s'+'cript>';
document.write(tag);

for (i = 0, i < Green.length, i++){
    document.write(tag_two[i]);
}
document.write(tag_three);
</script>
</body>
</html>

Thanks!

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
Kamui
  • 719
  • 1
  • 9
  • 16
  • Does this help? http://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript – Ariane Jul 12 '13 at 02:25
  • I will take a look at it, but it's a little more complex than what I know. I have close what I think will work, I just have trouble joining the three different parts of the URL together to get it to work. – Kamui Jul 12 '13 at 02:34
  • I'm sorry, I can't help further. You're doing stuff I don't understand very well (and am too tired to feel like trying to understand)... I just thought the most logical way to put variables in an URL using Javascript was to have it put values in GET, so I just looked for a solution towards that. If your thing works, then it's fine. Though I can't help but think that it's going to make pretty odd URLs. But that's not the issue here. – Ariane Jul 12 '13 at 02:39

2 Answers2

1

I have found some problems in your code. so I have corrected them and here is you code.

<html>
<body>

<script>
var orderID = 50;
var Red = 100;
var Blue = 200;
var Green = new Array(Math.floor(Math.random() * 10 * 10), Math.floor(Math.random() * 10 * 10), Math.floor(Math.random() * 10 * 10));
var Yellow = 400;

var tag = '<s'+'cript language="JavaScript" src="http://www.test.com/test/Test_1/ato.' + orderID + '/[atc1.' + Red + '/atc2.' + Blue + '/atc3.' + Green[0];

i = 1;
var tag_two=new Array();
while (i < Green.length){

tag_two[i] ='/atc3.' + Green[i];
i++;
}

var tag_three ='/atc4.' + Yellow + ']"></s'+'cript>';


document.write(tag);

for (i = 0; i < Green.length; i++){

document.write(tag_two[i]);

}

document.write(tag_three);
</script>

</body>
</html>

The way you adding variables to url seems to be correct. Variables can be passed by using simple concatenate operator '+'

Haris Amjed
  • 171
  • 1
  • 7
1

Instead of creating the new variable , you have to concatinate the string to the existing variable :

here is the working example http://jsfiddle.net/afsar_zan/aAbD6/

sample code

       var orderID = 50;
        var Red = 100;
        var Blue = 200;
        var Green = [Math.floor(Math.random() * 10 * 10), Math.floor(Math.random() * 10 * 10), Math.floor(Math.random() * 10 * 10)];
        var Yellow = 400;
        var i = 0;
        var tag = '<script language="JavaScript" src="http://www.test.com/test/Test_1/ato.' + orderID + '/[atc1.' + Red + '/atc2.' + Blue + '/atc3.' + Green[0];

        while (Green.length>i){
         tag  += '/atc3.' + Green[i];
            i++;
        }
        tag +='/atc4.' + Yellow + ']"></s'+'cript>';
        alert(tag);
Afsar
  • 3,104
  • 2
  • 25
  • 35