Forum Moderators: open

Message Too Old, No Replies

Pick random value from an array

         

zootreeves

11:53 am on Aug 16, 2004 (gmt 0)

10+ Year Member



Hi,

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"

Bernard Marx

12:58 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few issues here. Some aren't particularly important, but I'll mention them anyway.

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.
Here's an example. This one doesn't bother with a function to get a random array member:

keywords = 
[
"ppc",
"games",
"advertise",
"meta",
"home",
"gaming",
"welcome"
]
var keyword = keywords[Math.floor(Math.random()*keywords.length)]

document.write(keyword);

ToneLeMoan

1:04 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Try this:

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

zootreeves

3:00 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Hi,

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>

Bernard Marx

4:58 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




No (!). Javascript won't resolve inside a tag like that.
You can try something along these lines (keep an eye on the quoting.)

document.write(
'<script src="http://domain.com/feeds/javascript.php?keyword=' + keyword + '&count=5"></script>'
)

zootreeves

9:14 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Nope that won't work

Bernard Marx

9:27 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it will! (unless I've missed an inverted comma somewhere). It's a common practice.
Could you be a little more specific than "Nope that won't work"?

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?

zootreeves

6:39 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



This script:

<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

Bernard Marx

9:21 pm on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry. The error is caused by the "</script>" ,that is really part of the doc.write string, being parsed as HTML, prematurely ending the script block it's contained in. One solution is to to escape the slash, so:

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]

zootreeves

11:47 am on Aug 18, 2004 (gmt 0)

10+ Year Member



Fanastic it works!

Thank you
Ben Reeves