0

I've tried searching for several hours but cannot find an answer that works. I want to load a different image for each day of the year.

I have some js that creates a variable based on the date function. I concatenate this with other text strings to get this variable:

photo2load = http://www.example.com/photos/pic132.jpg

How do I then get this photo to load. What is the scripting required? I'm at a total loss.

Thanks in advance.

user2864740
  • 60,010
  • 15
  • 145
  • 220

1 Answers1

1

You need to add an id attribute to your image and then onload call a function with inside something like this:

function loadMyImage(){
    var img = document.getElementById("id-of-image");
    img.src = photo2Load;
}

Or if you want to pass directly your variable call the function like this

loadMyImage( photo2Load);

function loadMyImage(imgUrl){
    var img = document.getElementById("id-of-image");
    img.src = imgUrl ;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
keypaul
  • 497
  • 8
  • 12
  • Hi Paul, sorry about the delay in getting back. I'm very new at JS and I'm unsure how I get the ID of an image that's not currently loaded. – user3629707 May 13 '14 at 18:55
  • Read here http://stackoverflow.com/questions/263359/how-can-i-determine-if-an-image-has-loaded-using-javascript-jquery – keypaul May 13 '14 at 21:10