A different image will appear at the appropriate point in the page every time the page is loaded or reloaded.
// CHANGE THIS to the number of images that exist.
// YOU MUST NAME YOUR IMAGE FILES image1.jpg, image2.jpg,
// image3.jpg, etc. up to and INCLUDING this number.
images = 7;
number = Math.floor(Math.random(1) * images) + 1;
document.write("<img src=\"image" + number + ".jpg\"/>");
"But what if I want the image to be a link?"
That's easy! Just remember that the code output by document.write becomes part of the page exactly as if it had appeared at the point in the page where the script element is located.
If you had an ordinary <img> element, you would wrap a link around it like this:
So to wrap a link around a randomly chosen image, we simply put both the script and the noscript elements inside our a element, like this:
<!-- Open the link -->
... Script code goes here as shown above
... Alternative image goes here
<!-- Close the link -->
Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!