Author Topic: Call random images  (Read 282 times)

mdahlke

  • Senior Member
  • ****
  • Posts: 179
  • Karma: 7
    • View Profile
    • Rummage City!
Call random images
« on: May 10, 2012, 08:29:10 AM »
I'm trying to create a script to call 5 random images for a "captcha type thing"

this is the code I have

#                  function showImg() {
                  
                  var imgNumber = randomInteger(9);
                  
                  document.write('<img src=" + imgNumber + " alt="" />');

                  }
#

That is in the <head>

#
function randomInteger(size) {
   return Math.floor((size+1)*Math.random());
}
#

That is in an external file

#
  <td colspan="2" class="center">
            
               <script type="text/javascript">
               showImg();
               showImg();
               showImg();
               showImg();
               showImg();
            </script>

               </td>
#

And then that is where I want to call that function 5 time to produce 5 random images

What am I missing?
If I help you out why not bump up my Karma a bit?

Sarthak Patel

  • Senior Member
  • ****
  • Posts: 368
  • Karma: 6
    • View Profile
Re: Call random images
« Reply #1 on: May 12, 2012, 01:12:44 AM »
Quote
hello mdahlke,

i have checked your code and modify this use below code it will work for you.

Note: add correct path to imag src attribute.
for below script put images and script in same folder and run the code.

PHP Code: [Select]

<html>
	
<
head>
	
	
<
title>SR Logic</title>
	
</
head>
	
<
body>
	
	
<
script type="text/javascript">
	
	
	
function 
showImg() 
	
	
	
{
	
	
	
	
var 
imgNumber randomInteger(9);
	
	
	

	
	
	
	
document.write('<img src="' imgNumber '" alt="" />');
	
	
	
}
	
	
	
function 
randomInteger(size) {
	
	
	
   var 
Math.floor((size+1)*Math.random());
	
	
	
   return 
x+'.jpg';
	
	
	
}
	
	
</script>
	
</body>
	
	
<td colspan="2" class="center">
	
	
	
 <script type="text/javascript">
	
	
	
 
	
showImg();
	
	
	
 
	
showImg();
	
	
	
 
	
showImg();
	
	
	
 
	
showImg();
	
	
	
 
	
showImg();
	
	
	
 </script>
	
	
</td>
</html>




i hope this will helpful for you..
~~SR~~



mdahlke

  • Senior Member
  • ****
  • Posts: 179
  • Karma: 7
    • View Profile
    • Rummage City!
Re: Call random images
« Reply #2 on: May 13, 2012, 03:25:46 PM »
that worked perfectly, Thank you
If I help you out why not bump up my Karma a bit?