How To Load Random Pictures in a Cell as Image OR Background From A Directory
Hi,
If Any One Can give some script to change the background picture or image inside a cell for every 10 seconds randomly, If you can kindly send me. It will be more helpful
Thankyou,
Chock.
Re: How To Load Random Pictures in a Cell as Image OR Background From A Directory
you need to supply a list of possible pictures from the server folder, as you cannot peruse a folders contents on the client side.
Code:
var imageElement;
window.onload = init;
// assuming 'images' is an array containing the address of each image
function init()
{
imageElement = document.getElementById('image');
setInterval('changeImage', 10000);
}
var prevImageID = -1;
function changeImage()
{
do {
var imageID = Math.round(Math.random() * images.length);
} while (imageID != prevImageID);
imageElement.style.background = "url('" + images[imageID] + "')";
prevImageID = imageID;
}
The element whose background image is to be changed should have the ID "image". If you want to use a class name or tag name you will have to modify the code slightly to use an array of elements instead.
Re: How To Load Random Pictures in a Cell as Image OR Background From A Directory
Hi penagate,
Thank you for your reply. ForYourInformation I am designing this page in FrontPage and saving it as Html. I think i have to add your code in the top of the page. Should i call the 'var imageElement' inside the table's Cell like this <td imageElement> or what. i am now displaying the background picture as below
<img border="0" src="SAPSD%20Files/modImage-105.jpg" width="250" height="174">
At the same time i seen in a site, they are displaying the pictures in a cell from different direction, from right to left, left to right, top to bottom vice versa & diagnolly. But when i save that page & see, there's a object where a file name 05.swf is diplayed in the cell where it displays random picutres, i right click & cheked it property it shows "Movie In Flash Format Properties"
Kindly suggest how to use & code.
Thankyou,
Chock. :)
Re: How To Load Random Pictures in a Cell as Image OR Background From A Directory
The code needs to go in a <script> element within the <head> section. As follows.
HTML Code:
<head>
<!-- anything else you may have here ... -->
<script type="text/javascript">
var imageElement;
window.onload = init;
// assuming 'images' is an array containing the address of each image
function init()
{
imageElement = document.getElementById('image');
setInterval('changeImage', 10000);
}
var prevImageID = -1;
function changeImage()
{
do {
var imageID = Math.round(Math.random() * images.length);
} while (imageID != prevImageID);
imageElement.style.background = "url('" + images[imageID] + "')";
prevImageID = imageID;
}
</script>
</head>
For it to work, you will need an element onthe page with the id "image", this is the element whos background image willbe changed randomly. Also you will need to supply a list of possible image addresses in the form of a Javascript array called "images".
Re: How To Load Random Pictures in a Cell as Image OR Background From A Directory
Hi,
Thankyou for your replies. I will try this in my home, and come back to your on Saturday, because here its leave tommorrow. I hope i can resolve with your script.
Thankyou,
Chock.
1 Attachment(s)
Re: How To Load Random Pictures in a Cell as Image OR Background From A Directory
Hi,
I tried with your same code. But still strucked to display the image. I have attached the html page which is designed in FrontPage named Index1.html & all 3 jpeg files which i am trying to display randomly.Kindly can u check & correct where the mistakes is happen. If possible do the correction and send me. Strucked up with this for a long time. I written the function. I think the problem is when displaying the image only. For my code it didn't display any messages. If i give the input statemetn inside the <TD> then its showing a blank image. Need Your Help (.|.)
Thankyou,
Chock. :)