Welcome to WebmasterWorld Guest from 54.80.123.20

Forum Moderators: open

Message Too Old, No Replies

Pick random value from an array

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

Junior Member

10+ Year Member

joined:Jan 23, 2004
posts:81
votes: 0


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"

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


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

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

New User

10+ Year Member

joined:July 28, 2004
posts:31
votes: 0


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

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

Junior Member

10+ Year Member

joined:Jan 23, 2004
posts:81
votes: 0


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>

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0



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>'
)
9:14 pm on Aug 16, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 23, 2004
posts:81
votes: 0


Nope that won't work
9:27 pm on Aug 16, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


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?

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

Junior Member

10+ Year Member

joined:Jan 23, 2004
posts:81
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


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]
11:47 am on Aug 18, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 23, 2004
posts:81
votes: 0


Fanastic it works!

Thank you
Ben Reeves