954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can I display a random array in more than one spot with random results?

Simple question, I'll take NO for an answer if it's not possible.

With a random array, can I display the results multiple time on the same page. I know I can't use css, and I would rather not use php... but is there a way for me to list the script below, then just punch in a div class at several points throughout my site?

I'm using this for advertising, and I'd like to be able to display the random image and link at 10 different points on the same page. Then to edit, I'd only want to edit the one single source. I guess it would kinda be like a slot machine, and I wouldn't mind a situation where they'd all end up as the same image :-)

Any help would be great. Thanks.

<script type="text/javascript">

//Javascript Created by Computerhope http://www.computerhope.com

//store the quotations in arrays

var images = [], 
index = 0;

images[0] = "<a href = 'http://www.computerhope.com/index.htm'><img src='http://www.computerhope.com/banners/banner.gif' alt='Visit Computer Hope'></a>";

images[1] = "<a href = 'http://www.computerhope.com/history/index.htm'><img src='http://www.computerhope.com/banners/banner2.gif' alt='Computer History'></a>";

images[2] = "<a href = 'http://www.computerhope.com/index.htm'><img src='http://www.computerhope.com/banners/banner3.gif' alt='Visit Computer Hope'></a>";

images[3] = "<a href = 'http://www.computerhope.com/newslet.htm'><img src='http://www.computerhope.com/banners/banner4.gif' alt='Computer Hope Newsletter'></a>";

index = Math.floor(Math.random() * images.length);

document.write(images[index]);

//done

</script>
jonsan32
Junior Poster in Training
53 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

I believe a better way to state this question is: "Can I echo an array without the use of php?"

jonsan32
Junior Poster in Training
53 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: