Forum Moderators: open
I have a couple of images something like a banner .....each focussing on some important aspects that i want to project through my site... i want to find out a way by which a different image is displayed each time the page is refreshed....in other words..the images (banner) should dynamically change on the webpage.... can this be done and how!?
[webmasterworld.com...]
you just have to change a few things in the code, instead of using document.getElementById() on the id of the thing, you'd just asign the array value to a variable and then use document.write to write the image tag using that variable as the url, make sure to include a <noscript>image</noscript> so the page can default to something if no javascript.
example:
<html>
<head>
<title></title>
<script type="text/javascript">
function random_image()
{
image_array = new Array();
image_array[0] = '/images/image02.jpg';
image_array[1] = '/images/image03.jpg';
image_array[2] = '/images/image04.jpg';
count = image_array.length;
random = Math.floor(Math.random() * count);
document.write( "<img src=\"" + image_array[random] + "\">");
}
</script>
</head>
<body>
<script>random_image()</script>
</body>
</html>