benshepherd

Untitled

Jul 14th, 2013
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.74 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Random Image</title>
  5.     <style>
  6.         img {
  7.             width:150px;
  8.             height:150px;
  9.             border:1px solid black;
  10.         }
  11.     </style>
  12.     <script>
  13.            
  14.         //Red images array
  15.         var redImages = ["./images/red/red001.jpg", "./images/red/red002.jpg", "./images/red/red003.jpg", "./images/red/red004.jpg", "./images/red/red005.jpg", "./images/red/red006.jpg"];
  16.         //Blue images array
  17.         var blueImages = ["./images/blue/blue001.jpg", "./images/blue/blue002.jpg", "./images/blue/blue003.jpg", "./images/blue/blue004.jpg", "./images/blue/blue005.jpg", "./images/blue/blue006.jpg"];       
  18.        
  19.        
  20.         window.onload = function() {
  21.         //When the document has loaded, get random pictures
  22.             RandomBluePicture();
  23.             RandomRedPicture();
  24.         };
  25.        
  26.         function RandomBluePicture() {
  27.    
  28.             var img = blueImages[ randomInt(0, blueImages.length - 1) ];
  29.             var tag = document.getElementsByTagName("img")[0]; //First image tag
  30.            
  31.             alert("Image selected: " + img);
  32.            
  33.             tag.setAttribute("alt", img);
  34.             tag.setAttribute("src", img);
  35.             tag.setAttribute("title", img);
  36.            
  37.         }
  38.         function RandomRedPicture() {
  39.        
  40.             var img = redImages[ randomInt(0, redImages.length - 1) ];
  41.             var tag = document.getElementsByTagName("img")[1]; //Second image tag
  42.            
  43.             alert("Image selected: " + img);
  44.            
  45.             tag.setAttribute("alt", img);
  46.             tag.setAttribute("src", img);
  47.             tag.setAttribute("title", img);                
  48.         }
  49.        
  50.         function randomInt(Min, Max) {
  51.             return Math.floor(Math.random() * (Max - Min + 1) + Min);
  52.         }
  53.  
  54.     </script>
  55. </head>
  56.  
  57. <body>
  58.     <button onclick="RandomBluePicture()">Click to get random blue picture</button><br>
  59.     <img>
  60.     <p>&nbsp;</p>
  61.     <button onclick="RandomRedPicture()">Click to get random red picture</button><br>
  62.     <img>
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment