1

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

2
  • 2
    Remove the parenthesis "()" at the end of img.src assignment. Commented Jan 23, 2012 at 2:22
  • 1
    Agree with @Abbas - also remove the leading "(" from the dd assignment and (optional) leading and trailing parentheses from the mm assignment. Commented Jan 23, 2012 at 2:25

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the alert was useful, I missed a character in the path. <body onload="LoadPage()"> completed the task.I cannot vote the answer because I'm new.
0

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")?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.