kahuna
kahuna

Reputation: 49

IMG SRC and Javascript

I'm new to javascript and I'm trying to learn. I'd like my webpage to dynamically show an image from another parent site based on the actual data. I tried to create the function to get the variables I need in order to get the image at the correct url I read Variable for img src= but I got lost, then I tried to do something like this

function LoadPage() { 
var today = new Date(); 
var yyyy = today.getFullYear(); 
var mm = (today.getMonth()+ 1); 
var dd = (today.getDate(); 

img.src = "http://www.parentsite.com/"+yyyy+"_"+mm+"_"+dd+"/images.png"();
}

</script>

Could anyone please help me with this? Thank you

Upvotes: 1

Views: 821

Answers (2)

Cybercartel
Cybercartel

Reputation: 12592

The var img needs to be a pointer to an asset for example an image so that you can assign the asset a src attribute with the method setAttribute("src",url). Maybe you have forgot to assign img = getElementByName("myasset")?

Upvotes: 0

box86rowh
box86rowh

Reputation: 3415

function LoadPage() { 
var today = new Date(); 
var yyyy = today.getFullYear(); 
var mm = (today.getMonth()+ 1); 
var dd = today.getDate(); 
var url = "http://www.parentsite.com/"+yyyy+"_"+mm+"_"+dd+"/images.png";
alert(url);
document.getElementById("img").setAttribute("src",url);
}

Assuming your image tag has the id img, the alert line should tell you wether you are setting up your image path correctly

Upvotes: 1

Related Questions