Picnic Website Code Tutorials

jQuery Random Image On Page Refresh

5/07/2013

View Demo

randomImage.generate

"How to create random images on page refresh with jQuery".

jQuery

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript">
<script>
      var randomImage = {
        paths: [
          "http://lorempixel.com/400/400/cats/1",
          "http://lorempixel.com/400/400/cats/2",
          "http://lorempixel.com/400/400/cats/3",
		  "http://lorempixel.com/400/400/cats/4",
		  "http://lorempixel.com/400/400/cats/5",
        ],
        generate: function(){
          var path = randomImage.paths[Math.floor(Math.random()*randomImage.paths.length)];
          var img = new Image();
          img.src = path;
          $("a.random").html(img);
          $("a.random").attr("href", path);
        }
      }
      randomImage.generate();
</script>
		

HTML

<a href="" class="random"></a>
		

Need help adding this or other code to your website? Post it here in the forum! Forum

Sponsors

Top Donators

Friends of Mine