html - Randomize the image on reload - javascript -
i have html page of 9 images must change order when page reloaded have placed image 3 rows , 3 columns on reload should change order of image .
here html code
<html> <head> <script type="text/javascript"> var len = document.images.length; var images = document.images; var img = function (){ for(var j, x, = images.length; i; j = parseint(math.random() * i), x = images[--i], images[i] = images[j], images[j] = x); } window.onload = function(){ for(var = 0 ; < len ; i++) { images[i].src = img[i].src; } } </script> </head> <body> <table> <tr> <td id="cell1"><button value="1"><img src="1.jpg" width="42" height="42"/></button></td> <td id="cell2"><button value="2"><img src="2.jpg" width="42" height="42"/></button></td> <td id="cell3"><button value="3"><img src="3.jpg" width="42" height="42"/></button></td> </tr> <tr> <td id="cell4"><button value="4"><img src="4.jpg" width="42" height="42"/></button></td> <td id="cell5"><button value="5"><img src="5.jpg" width="42" height="42"/></button></td> <td id="cell6"><button value="6"><img src="6.jpg" width="42" height="42"/></button></td> </tr> <tr> <td id="cell7"><button value="7"><img src="7.jpg" width="42" height="42"/></button></td> <td id="cell8"><button value="8"><img src="8.jpg" width="42" height="42"/></button></td> <td id="cell9"><button value="9"><img src="9.jpg" width="42" height="42"/></button></td> </tr> </table> <!-- forms's action sends data specified php page --> <form action="pictures.php" method="post"> <input id="pswd" type="hidden" value="" name="pass"> </form> </body> </html>
i cant randomize image . suggestion on doing wrong :)
you should "randomize" array:
<script type="text/javascript"> var len = document.images.length; var images = document.images; var img = function (){ for(var j, x, = images.length; i; j = parseint(math.random() * i), x = images[--i], images[i] = images[j], images[j] = x); } img = shuffle(img);//this "randomizes" 'img' array using function bellow window.onload = function(){ for(var = 0 ; < len ; i++) { images[i].src = img[i].src; } </script>
and paste function in code: https://stackoverflow.com/a/2450976/3132718
Comments
Post a Comment