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!

4

2 に答える 2

1

新しい変数を作成する代わりに、文字列を既存の変数に連結する必要があります。

これが実際の例ですhttp://jsfiddle.net/afsar_zan/aAbD6/

サンプルコード

       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);
于 2013-07-12T02:51:32.300 に答える
1

あなたのコードにいくつかの問題が見つかりました。私はそれらを修正しました。これがあなたのコードです。

<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>

URLに変数を追加する方法は正しいようです。単純な連結演算子「+」を使用して変数を渡すことができます

于 2013-07-12T02:46:15.597 に答える