Jump to content

Random Image Selection


Guest Cataclysm_J

Recommended Posts

Guest Cataclysm_J

Hi,If I had a folder of images could I use javascript to get it to randomly select one and put into into a position on my webpage?If so how what would be the code involved? I'm quite used to HTML and handy with it but I know very little javascript so any help would be much appreciated.

Link to comment
Share on other sites

I think I can help you.It's a little hard to explain it, but I'll try. Put the <script> in the <head> tag and the <img> wherever you want it to be on you document. All you have to do is change the imageURL1-10.png of the "if" and "else if" tags in the javascript to whatever your picture URLs are. Note that I'm using the simplest javascript in this so that you can look all this up on W3schools if you want to and that in this script that I wrote you can only have 10 images that are randomly selected. If you want to have more or fewer, just reply and someone else or I will give you the revised code.

<html><head><script type="text/javascript">\\The script is started.var a=math.random()\\The variable for the random selection.\\I'm going to be making this for 10 pictures.\\If you want more or less, just reply so I can change it for you.if (a <= .1){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL1.png"}else if (a<=.2,>.1){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL2.png"}else if (a<=.3,>.2){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL3.png"}else if (a<=.4,>.3){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL4.png"}else if (a<=.5,>.4){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL5.png"}else if (a<=.6,>.5){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL6.png"}else if (a<=.7,>.6){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL7.png"}else if (a<=.8,>.7){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL8.png"}else if (a<=.9,>.8){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL9.png"}else if (a<=1,>.9){document.getElementById("pleasedontchangeunlessyouchangealltheotheronestoo").src="imageURL10.png"}</script></head><body><img src="thisURLdoesntmatter.cool" id="pleasedontchangeunlessyouchangealltheotheronestoo" /></body></html>

I think you can figure out whatever I didn't explain. Reply if you need any help.Something else--the tenth image will have about 1/100,000,000 chance LESS than all the others of coming up. It's not nearly anything that any normal person visiting your webpage would ever even look for, let alone notice. I just thought it would be cool to let you know.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...