Can I randomly search an array in a .js file not on my page?

M

mdh2972

If I have a .js file filled with data like links. how do i access just
1 element [random] and bring it on my page
 
E

Evertjan.

wrote on 09 jan 2007 in comp.lang.javascript:
If I have a .js file filled with data like links. how do i access just
1 element [random] and bring it on my page

It is not a js-file, I presume.

Show us some code.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Tue, 9 Jan 2007 10:30:42, (e-mail address removed) posted:
If I have a .js file filled with data like links. how do i access just
1 element [random] and bring it on my page

Make the file include function Random() from the FAQ, and

var MyStuff = [
"Link 0",
"Link 1",
// ...
"Link 9",
"Dummy" ]

and

function getRand(X) { return X[Random(X.length-1)] }

then in your page

SomeLink = getRand(MyStuff)

Include the .js file in the usual manner, as in <URL:http://www.merlyn.
demon.co.uk/js-nclds.htm>.

You may prefer to have one .js file for the code and others for data.

"Dummy" was included for ease of editing; it allows all the links lines
to be of the same format. If you omit it, omit also -1.

It's a good idea to read the newsgroup and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top