This is a forum where members new and old can come to ask questions and get info and opinions. It is not a place to advertise your business or have other forms of advertising, whether it be in your posts or signature.

All links in the forum will not be indexed by Search Engines and any unapproved forms of advertising or spam will be dealt with accordingly, deleted, and that member's account banned.

Forums

Overview » Ajax & JavaScript » Random image from a folder
Reply

Random image from a folder

Helle M
Freaking Member



Since: 17 Feb 2001
Posts: 110
Posted 07 Mar 2009 17:25:30

Does anybody know where to get a code that will show one different image everytime the page loads. Images should come from a specific folder that I choose ....
The site does not support any database, that is why I need this script.

thanks Helle
Edited by - Helle M on 07 Mar 2009  17:26:28

Does anybody know where to get a code that will show one different image everytime the page loads. Images should come from a specific folder that I choose ....
The site does not support any database, that is why I need this script.

thanks Helle
Edited by - Helle M on 07 Mar 2009  17:26:28
Georgi Kralev
Zone Administrator



Since: 15 Nov 2006
Posts: 1,422
Replied 20 Mar 2009 21:53:39
Hi Helle,

There is a random JavaScript function which could be used for this purpose.
The following example gives sample solution to your issue. It shows a random image from five images every time the page loads. It looks for the images in image folder and expects the images to have the following names: image_0.jpg, image_1.jpg etc.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Random Image</title>
</head> 
<body>

<img src="images/image_0.jpg" align="middle" border="0" name="RandomImg" >

<script language="JavaScript">
<!--
// Genarate random value from 0-5
var rand_no = Math.floor(6*Math.random());

// This defines the source of the preview image  (For example images/image_0.jpg)
document.images['RandomImg'].src="images/image_" + rand_no + ".jpg";
// -->
</script>  
</body>
</html>


Hope this helps.

Reply to this topic

Message
Reply
Follow us on Facebook Follow us on twitter Subscribe to the RSS feed
Activate your free membership today | Login | Currency