mardi 16 janvier 2018

jquery random image issue

I am trying to load 1 of 2 images at random on load of my home page. (ico.edu) - Currently, only 1 image loads (I want the 2nd image to load "randomly") - so sometimes users get to my website and image 1 loads, other times it's image 2. So far, only image 1 loads.

Here is the jQuery inside my php page that should load the random image, but nothing I change in the code seems to work. Help? (PS - this website is built with WordPress so this is custom code added to the WordPress homepage.php in order to customize the theme used....

<script>
jQuery(document).ready(function($){

var number = 1 + Math.floor(Math.random() * 2);    
var randimg;

if (number === 1) {
    randimg  = "http://ift.tt/2Dhrmy9";
} else { randimg  = "http://ift.tt/2B6pylq"; }


$('img').
filter(function() {
    return this.src.match("http://ift.tt/2Dhrmy9"); //if image caching is off
    //return this.src.match("http://ift.tt/2Dfqav2"); //with image caching version
})
.attr("src", randimg);

});
</script>




Aucun commentaire:

Enregistrer un commentaire