Skip to main content
Inspiring
May 16, 2013
Question

How to make a random image appear on a page using Javascript

  • May 16, 2013
  • 1 reply
  • 886 views

I've got an array of eleven images that I wish to show randomly on the index page in a site.  The image should change with every visit to the page.  I've tried various javascript "solutions" I've found on line, but none seem to work.  I've got the array in the head, with the complete path to the image on each element. 

How do I randomize the display, and how do I cause the image to appear in the div I've got set for the image?

Gene

This topic has been closed for replies.

1 reply

Participating Frequently
May 17, 2013

If you've tried various solutions and none have worked, then we need to figure out what you are doing wrong. Can you post the code that you have tried?

By the way, this forum is for server side applications, not client side.

Inspiring
May 17, 2013

Well, since I'm putting as much in the head as possible, it will be done serverside, I believe.  If I'm wrong, I apologize.  I can't post the code because the three examples I got off the internet do not work at all.  There's a page up on our server now that has the code as it sits, but that's only the array there. 

So, I'll go look elsewhere.

Gene

Participating Frequently
May 17, 2013

>Well, since I'm putting as much in the head as possible, it will be done serverside, I believe.

I'm not sure what you are trying to say here. Using a server side solution is a matter of inserting server side code, not how much code you are putting in the head. Image rotation can be done with either client or server side code.

>I can't post the code because the three examples I got off the internet do not work at all.

You can't post the code that isn't working? Why not?