Let's say I have am dynamically passing information to a variable in a URL like as shown below:
<script>
var ORDER = 200;
var QUANTITY = 1;
var EXTRA = [200,300,400];
var tag = document.write('<scr'+'ipt language="JavaScript" src="http://test.com/test/order.' + ORDER + '/quantity.' + QUANTITY"></scr' + 'ipt>');
</script>
Let's say I want to pass all the data in the EXTRA array... how would I do this?
I'm trying to get a URL that looks something like this after it is written to the page:
http://test.com/test/order.200/quantity.1/extra.200/extra.300/extra.400
(Passing the numbers to the same extra parameter in the URL is intentional, I need it to be passed in seperate instances but to the same variable)
I know I can use a for loop to cycle through the array.. how can I keep dynamically appending the numbers in the EXTRA array to the URL like in the example above?
Would something like this work?
for (i = 0; i < EXTRA.LENGTH; i++){
tag.append(EXTRA[i]);
}
Please advise if you can,
Thanks for your help!
This looks like a question about joining items in an Array. JavaScript has Array.prototype.join
for this purpose, so you would want to do
var str = 'foo';
if (EXTRA.length) str += '/extra.' + EXTRA.join('/extra.');
str; // "foo/extra.200/extra.300/extra.400"