Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

Random Image Generator

Recommended Posts

I have a javascript that generates random images when a page loads. It works fine, only the issue that I am having is that I have not figured out how to get it to display the name if each image as it is displayed on the screen. below is the script:


<SCRIPT LANGUAGE="Javascript">

<!-- Begin

var interval = 2.5; // delay between rotating images (in seconds)

var random_display = 1; // 0 = no, 1 = yes

interval *= 1000;

var image_index = 0;

image_list = new Array();

image_list[image_index++] = new imageItem("images/3channelorbal.jpg");


var number_of_image = image_list.length;

function imageItem(image_location) {

this.image_item = new Image();

this.image_item.src = image_location;


function get_ImageItemLocation(imageObj) {



function generate(x, y) {

var range = y - x + 1;

return Math.floor(Math.random() * range) + x;


function getNextImage() {

if (random_display) {

image_index = generate(0, number_of_image-1);


else {

image_index = (image_index+1) % number_of_image;


var new_image = get_ImageItemLocation(image_list[image_index]);



function rotateImage(place) {

var new_image = getNextImage();

document[place].src = new_image;

var recur_call = "rotateImage('"+place+"')";

setTimeout(recur_call, interval);



// End -->


Share this post

Link to post
Share on other sites

Do you mean that you want to display the file name next to the file?

Probably the easiest way to get the file name is to get the value of the src attribute of the img element, and then parse the string to get the part you want (i.e., the file name without the path to the file).


The following is a quick example of how to alert the src attribute value of all the img elements on a page:


var imgs = document.getElementsByTagName('img');

for (var i = 0, len = imgs.length; i < len; i++) {




Once you have the src attribute strings, don't forget to parse them as need be.


If I misunderstood your question, please let me know.


Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...