Forum Moderators: open
I'm trying to get javascript to pick a random value from an array, here is what i have got so far: (it doesn't work)
KeywordArray = new Array(7);
KeywordArray[1] = "ppc";
KeywordArray[2] = "games";
KeywordArray[3] = "advertise";
KeywordArray[4] = "meta";
KeywordArray[5] = "home";
KeywordArray[6] = "gaming";
KeywordArray[7] = "welcome";
var keyword = randgen(KeywordArray);
document.write("" + keyword + ");
I'm not very good with javascript, so it's probably totally wrong, it just print "undefined"
There's almost never a need to declare the length of a Javascript array when you create it. I won't go into details here, just take my word for it. Array indexing is "zero-based" - the first element has index, 0. Currently, your resulting array, actually has a length of 8 (not 7), as the 0th member is simply undefined.
It's actually easier to create the array using literal notation (later).
var keyword = randgen(KeywordArray);
Since you haven't posted the function,
randgen, there's no way of telling what the problem is. keywords =
[
"ppc",
"games",
"advertise",
"meta",
"home",
"gaming",
"welcome"
]
var keyword = keywords[Math.floor(Math.random()*keywords.length)]document.write(keyword);
KeywordArray = new Array(7);
KeywordArray[0] = "ppc";
KeywordArray[1] = "games";
KeywordArray[2] = "advertise";
KeywordArray[3] = "meta";
KeywordArray[4] = "home";
KeywordArray[5] = "gaming";
KeywordArray[6] = "welcome";
randno = Math.floor ( Math.random() * KeywordArray.length );
document.write(KeywordArray[randno]);
Oh I see someone beat me to it ;)
Thanks very much for your help, but i'm stuck again. Would it be possible to do something along the lines of:
keywords = [ "ppc", "games", "advertise", "meta", "home", "gaming", "welcome" ]
var keyword = keywords[Math.floor(Math.random()*keywords.length)]
<script src="http://domain.com/feeds/javascript.php?keyword=" + keyword + "&count=5"></script>
What is the rest of the code you are trying?
What does the error say? (if you get one).
Type this into the address bar:
javascript:'<xmp>' + document.documentElement.outerHTML + '</xmp>'
How does the 'internal' source code look?
<script>
keywords = [ "ppc", "games", "advertise", "meta", "home", "gaming", "welcome" ]
var keyword = keywords[Math.floor(Math.random()*keywords.length)]
document.write(
'<script src="http://domain.com/feeds/javascript.php?inc=&adultfilter=&paid=&q=' + keyword + '&count=5&ws="></script>'
)
</script>
just produces ' )
Heres the source:
<HTML><HEAD>
<SCRIPT>
keywords = [ "ppc", "games", "advertise", "meta", "home", "gaming", "welcome" ]
var keyword = keywords[Math.floor(Math.random()*keywords.length)]
document.write(
'<script src="http://advertise.veoda.com/feeds/javascript.php?inc=&adultfilter=&paid=&q=' + keyword + '&count=5&ws="></SCRIPT>
</HEAD>
<BODY>' ) </SCRIPT></BODY></HTML>
Thank you for your help
document.write(
'<script src="http://domain.com/feeds/javascript.php?inc=&adultfilter=&paid=&q=' + keyword + '&count=5&ws="><[blue]\[/blue]/script>'
) Some people would go further, and split the string down further, eg:
[pre]document.write(
'<scr'+'ipt src="http://domain.com/feeds/javascript.php?inc=&adultfilter=&paid=&q=' + keyword + '&count=5&ws="><\/scr'+'ipt>'
)[/pre]